Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Toy v1 parser implementation #162

Merged
merged 30 commits into from
Oct 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
4b5a41b
Trying to test toy-v1 with a v3x/ParseHelper (ANTLR) while reusing th…
rturrado Sep 11, 2023
3b65d4e
Refactored v3x/ParseHelper and implemented it using a ScannerAntlr.
rturrado Sep 12, 2023
502b607
Started implementing the toy v1 ANTLR parser.
rturrado Sep 13, 2023
c4f5612
Amend of previous commit.
rturrado Sep 14, 2023
270a6a0
Finished writing the builder visitor (ANTLR AST to tree-gen AST).
rturrado Sep 14, 2023
9b96146
Changed ScannerAntlr to have a build visitor as a dependency injection.
rturrado Sep 15, 2023
acb42c5
Changed test/v1x/parsing.cpp to accommodate toy v1x tests.
rturrado Sep 16, 2023
b46805c
Fixed ParsingTest::TestBody.
rturrado Sep 16, 2023
232e692
Trying to fix MacOS and Windows tests.
rturrado Sep 16, 2023
83febaf
Trying to fix MacOS compilation.
rturrado Sep 16, 2023
4a932fe
Added Toy v1 res/cq files.
rturrado Sep 16, 2023
5f0f2d5
Removed some Flex/Bison warnings due to integral types redefinitions.
rturrado Sep 18, 2023
a9013e7
WIP. [skip ci]
rturrado Sep 19, 2023
37eef7b
Updated v3x parser code (ParseHelper, ScannerAntlr, AST builder, erro…
rturrado Sep 20, 2023
0d07e5e
Removed cqasm::v3x::default_analyzer. Just reusing cqasm::v1x::defaul…
rturrado Sep 20, 2023
3fd53bd
Used make_unique instead of unique_ptr.
rturrado Sep 20, 2023
fd327ef
Removed temporary comments.
rturrado Sep 20, 2023
4cefec0
Updated README.md.
rturrado Sep 21, 2023
27a104a
Addressed some of the comments from Pablo's code review.
rturrado Sep 22, 2023
e2e4b81
Trying to fix MacOS build error.
rturrado Sep 22, 2023
0e6b951
Updated tree-gen commit ID, now to one in master.
rturrado Sep 22, 2023
60db46c
Addressed some of the comments from Pablo's code review.
rturrado Sep 26, 2023
d0a4e0f
Changed grammar to use alternative labels.
rturrado Sep 26, 2023
1535365
Added asserts for checking the type of the terminal node at get_integ…
rturrado Sep 27, 2023
56f6dff
Updated README.md (added 'WIP: cQASM v3.0' section) to explain that t…
rturrado Sep 27, 2023
a920f6e
Updated tree-gen commit ID, now to one in master.
rturrado Sep 27, 2023
9852a94
Merge remote-tracking branch 'origin/develop' into toy-v1-use_case_ci…
rturrado Sep 27, 2023
c5c67ec
Merge branch 'develop' into toy-v1-use_case_circuit_for_parser_verifi…
rturrado Sep 27, 2023
fbd83f7
Preparing everything for the merge.
rturrado Sep 29, 2023
a1a31db
BuildCustomAstVisitor class can be empty.
rturrado Sep 29, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ jobs:
run: |
sudo apt-get install -y swig
echo "LIBQASM_BUILD_TYPE=Debug" >> $GITHUB_ENV
- name: Install flex/bison and SWIG, and set build type (MacOS)
- name: Install SWIG, and set build type (MacOS)
if: matrix.os == 'macos-latest'
run: |
brew install swig
Expand Down
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ if(POLICY CMP0148)
cmake_policy(SET CMP0148 NEW)
endif()

project(cqasm C CXX)
project(cqasm LANGUAGES C CXX)

# If cqasm was already included elsewhere in the project, don't include it again.
# There should be only one place for it and one version per project.
Expand Down
74 changes: 36 additions & 38 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,55 +23,55 @@ The new API lives in `v1x` directories, and the older API in `v10` directories.

The following folders may be generated:

- `build/<build type>`: the C++ library outptu files generated by `conan`.<br/>
- `build/<build type>`: the C++ library output files generated by Conan.<br/>
- `pybuild`: the Python library output files generated by `setup.py`.

## Dependencies
### WIP: cQASM v3.0

* CMake >= 3.12
* conan 2.0
* gcc and g++ capable of C++20 standard
* git
* Python3

### ARM specific dependencies
We have started working on the parsing of cQASM v3.0 files, and that's why you will also see some `v3x` folders around.

* Flex >= 2.6.4
* Bison >= 3.0
* Java JRE >= Java 11
However, the development of the new parser is still in a very early stage so its use through the API is completely unsupported.

## Build
## Dependencies

This version of libqasm can only be compiled via `conan`.
* C++ compiler with C++20 support (gcc 11, clang 14, msvc 17)
* `CMake` >= 3.12
* `git`
* `Python` 3.x plus `pip`, with the following package:
* `conan` >= 2.0

### ARM specific dependencies

`conan` is a package manager that is very convenient for managing dependencies. It is installed via `pip`.
We are having problems when using the `m4` and `zulu-opendjk` Conan packages on an ARMv8 architecture.
`m4` is required by Flex/Bison and `zulu-openjdk` provides the Java JRE required by the ANTLR generator.
So, for the time being, we are installing Flex/Bison and Java manually for this platform.

```
pip install conan
```
* `Flex` >= 2.6.4
* `Bison` >= 3.0
* `Java JRE` >= 11

You'll need to create a default profile before using it:
## Build

```
conan profile detect
```
This version of `libqasm` can only be compiled via the Conan package manager.
You'll need to create a default profile before using it for the first time.

The installation of `libqasm` dependencies, as well as the compilation, can be done in one go.<br/>
The installation of dependencies, as well as the compilation, can be done in one go.

```
git clone https://github.com/QuTech-Delft/libqasm.git
cd libqasm
conan profile detect
conan build . -pr=conan/profiles/tests-debug -b missing
```

The command above is building `libqasm` in Debug mode with tests using the `tests-debug` profile.

The `-b missing` parameter asks `conan` to build packages from sources
Notice:
- the `conan profile` command only has to be run only once, and not before every build.
- the `conan build` command is building `libqasm` in Debug mode with tests using the `tests-debug` profile.
- the `-b missing` parameter asks `conan` to build packages from sources
in case it cannot find the binary packages for the current configuration (platform, OS, compiler, build type...).

### Build profiles


A group of predefined profiles is provided under the `conan/profiles` folder.
They follow the `[tests-](debug|release)[-compat]` naming convention. For example:
- `release` is a Release build without tests and compatibility with the original API.
Expand Down Expand Up @@ -124,15 +124,9 @@ python3 -m pytest
The `CMakeLists.txt` file in the root directory includes install targets:

```
conan create --version 0.4.1 . -s:h compiler.cppstd=20 -s:h libqasm/*:build_type=Debug -o libqasm/*:build_tests=True -b missing
conan create --version 0.4.1 . -pr=tests-debug -b missing
```

You may want to add one or more options to the `conan` command:

- <code><nobr>-o libqasm/*:compat=True</nobr></code>: enables installation of the headers for the original API, on top of the ones for the new API.
- <code><nobr>-o libqasm/*:shared=True</nobr></code>: builds a shared object library instead of a static library, if applicable.


You can test if it works by doing:

```
Expand All @@ -149,9 +143,7 @@ The new API doesn't have Python bindings yet.

### From C++

The easiest way to use libqasm in a CMake project is to fetch the library and then link against it.

Note that the Java JRE is required for libqasm to build:
The easiest way to use `libqasm` in a CMake project is to fetch the library and then link against it.

```
include(FetchContent)
Expand All @@ -164,12 +156,18 @@ target_include_directories(<your target> SYSTEM PRIVATE "${cqasm_SOURCE_DIR}/inc
target_link_libraries(<your target> PUBLIC cqasm)
```

Note that the following dependencies are required for `libqasm` to build:

* `Flex` >= 2.6.4
* `Bison` >= 3.0
* `Java JRE` >= 11

The original API headers are *not* included by default.
To enable those, pass <code><nobr>-o libqasm/*:compat=True</nobr></code> as a build option to Conan.

## Docker

This tests the library in a container with the bare minimum requirements for libqasm.
This tests the library in a container with the bare minimum requirements for `libqasm`.

```
docker build .
Expand Down
1 change: 1 addition & 0 deletions conanfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class LibqasmConan(ConanFile):
exports_sources = "CMakeLists.txt", "include/*", "python/*", "res/*", "scripts/*", "src/*", "test/*"

def build_requirements(self):
self.requires("fmt/10.1.1")
self.tool_requires("m4/1.4.19")
if self.settings.os == "Windows":
self.tool_requires("winflexbison/2.5.24")
Expand Down
50 changes: 26 additions & 24 deletions include/cqasm-version.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@

#include "cqasm-error.hpp"

#include <cstdint> // int64_t
#include <cstdio> // FILE*
#include <fmt/ostream.h>
#include <memory> // unique_ptr
#include <ostream>
#include <string>
#include <cstdint>
#include <complex>
#include <memory>
#include <vector>

/**
Expand Down Expand Up @@ -60,60 +62,59 @@ std::ostream &operator<<(std::ostream &os, const Version &object);


struct ScannerAdaptor {
virtual ~ScannerAdaptor() = default;
virtual ~ScannerAdaptor();

virtual void parse(const std::string &filename, Version &version) const = 0;
virtual void parse(const std::string &file_name, Version &version) const = 0;
};


class ScannerFlexBison : public ScannerAdaptor {
protected:
void *scanner_{ nullptr };
void parse_(const std::string &filename, Version &version) const;
void parse_(const std::string &file_name, Version &version) const;
public:
ScannerFlexBison();
~ScannerFlexBison() override;
void parse(const std::string &filename, Version &version) const override = 0;
void parse(const std::string &file_name, Version &version) const override = 0;
};


class ScannerFlexBisonFile : public ScannerFlexBison {
FILE *fp_{ nullptr };
public:
explicit ScannerFlexBisonFile(FILE *fp);
~ScannerFlexBisonFile() override = default;
void parse(const std::string &filename, Version &version) const override;
~ScannerFlexBisonFile() override;
void parse(const std::string &file_name, Version &version) const override;
};


class ScannerFlexBisonString : public ScannerFlexBison {
const char *data_{ nullptr };
public:
explicit ScannerFlexBisonString(const char *data);
~ScannerFlexBisonString() override = default;
void parse(const std::string &filename, Version &version) const override;
~ScannerFlexBisonString() override;
void parse(const std::string &file_name, Version &version) const override;
};


/**
* Parse the given file to get its version number.
* Parse the given file path to get its version number.
* Throws an AnalysisError if this fails.
*/
Version parse_file(const std::string &filename);
Version parse_file(const std::string &file_path);

/**
* Parse using the given file pointer to get its version number.
* Throws an AnalysisError if this fails.
* The file is rewound back to the start when parsing completes.
* A filename may be given in addition for use within the AnalysisError thrown when version parsing fails.
* A file_name may be given in addition for use within the AnalysisError thrown when version parsing fails.
*/
Version parse_file(FILE *file, const std::string &filename = "<unknown>");
Version parse_file(FILE* fp, const std::string &file_name = "<unknown>");

/**
* Parse the given string as a file to get its version number.
* A filename may be given in addition for use within the AnalysisError thrown when version parsing fails.
* A file_name may be given in addition for use within the AnalysisError thrown when version parsing fails.
*/
Version parse_string(const std::string &data, const std::string &filename = "<unknown>");
Version parse_string(const std::string &data, const std::string &file_name = "<unknown>");


/**
Expand All @@ -128,19 +129,20 @@ class ParseHelper {
/**
* Name of the file being parsed.
*/
std::string filename;
std::string file_name;

public:
/**
* Parse a file with flex/bison.
*/
explicit ParseHelper(std::unique_ptr<ScannerAdaptor> scanner_up, std::string filename = "<unknown>");
explicit ParseHelper(std::unique_ptr<ScannerAdaptor> scanner_up, std::string file_name = "<unknown>");

/**
* Does the actual parsing.
*/
Version parse();
};


} // namespace cqasm::version

/**
* std::ostream support via fmt (uses operator<<).
*/
template <> struct fmt::formatter<cqasm::version::Version> : ostream_formatter {};
1 change: 0 additions & 1 deletion include/v1x/cqasm-analyzer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,6 @@ class Analyzer {
* will be used only for error messages.
*/
AnalysisResult analyze_string(const std::string &data, const std::string &filename = "<unknown>") const;

};

} // namespace analyzer
Expand Down
2 changes: 1 addition & 1 deletion include/v1x/cqasm-ast.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@

#pragma once

#include "../cqasm-string-builder.hpp"
#include "cqasm-string-builder.hpp"
#include "v1x/cqasm-ast-gen.hpp"
Loading