Skip to content

Commit

Permalink
Updated v3x parser code (ParseHelper, ScannerAntlr, AST builder, erro…
Browse files Browse the repository at this point in the history
…r listener).

- src/CMakeLists.txt:
  - Added 'find_package(fmt 10.1.1)'.
  - Updated to a version of tree-gen that doesn't fetch fmt if the package is found.
- v3x/cqasm-parse-helper:
  - Changed ScannerAntlr to have a customer error listener as a dependency injection.
  - Changed the 'parse' API to return a ParseResult. Ideally, a ParseResult should be an interface containing a pointer to a generic AST, not to a tree-gen AST.
- v3x/BuildTreeGenAstVisitor: fixed so that all tests are now run.
- v3x/CustomErrorListener: added so that the parser throws an exception instead of just printing to standard output.
- res/v1x/toy-v1x-parsing:
  - All implemented tests pass the syntax analysis check.
  - Updated test_cases.txt.

Note:
 - conanfile.py: temporarily commented out a few lines to be able to work with CLion's Conan Plugin.
 - test/CMakeLists.txt: temporarily commented out version tests.
 - test/v1x/parsing.cpp: temporarily commented out semantic checks.
  • Loading branch information
rturrado committed Sep 20, 2023
1 parent a9013e7 commit 37eef7b
Show file tree
Hide file tree
Showing 86 changed files with 1,076 additions and 165 deletions.
8 changes: 4 additions & 4 deletions conanfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ def build_requirements(self):
self.tool_requires("bison/3.8.2")
if self.settings.arch != "armv8":
self.tool_requires("zulu-openjdk/11.0.19")
if self.options.build_tests:
self.requires("gtest/1.14.0")
#if self.options.build_tests:
self.requires("gtest/1.14.0")

def requirements(self):
self.requires("antlr4-cppruntime/4.13.0")
Expand All @@ -87,8 +87,8 @@ def layout(self):
self.cpp.build.libdirs = ["."]

def generate(self):
deps = CMakeDeps(self)
deps.generate()
#deps = CMakeDeps(self)
#deps.generate()
tc = CMakeToolchain(self)
tc.variables["ASAN_ENABLED"] = self.options.asan_enabled
tc.variables["LIBQASM_BUILD_PYTHON"] = self.options.build_python
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ namespace cqasm::v3x::parser {

class BuildCustomAstVisitor : public CqasmParserVisitor {
public:
std::any visitProgram(CqasmParser::ProgramContext *context) = 0;
virtual std::any visitProgram(CqasmParser::ProgramContext *context) = 0;
std::any visitVersion(CqasmParser::VersionContext *context) = 0;
std::any visitQubits(CqasmParser::QubitsContext *context) = 0;
std::any visitStatement(CqasmParser::StatementContext *context) = 0;
std::any visitMapping(CqasmParser::MappingContext *context) = 0;
std::any visitVariable(CqasmParser::VariableContext *context) = 0;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#include "v3x/BuildCustomAstVisitor.h"
#include "v3x/BuildCustomAstVisitor.hpp"
#include "v3x/CqasmParser.h"
#include "v3x/CqasmParserVisitor.h"

Expand All @@ -13,6 +13,7 @@ class BuildTreeGenAstVisitor : public BuildCustomAstVisitor {
public:
std::any visitProgram(CqasmParser::ProgramContext *context) override;
std::any visitVersion(CqasmParser::VersionContext *context) override;
std::any visitQubits(CqasmParser::QubitsContext *context) override;
std::any visitStatement(CqasmParser::StatementContext *context) override;
std::any visitMapping(CqasmParser::MappingContext *context) override;
std::any visitVariable(CqasmParser::VariableContext *context) override;
Expand Down
21 changes: 21 additions & 0 deletions include/v3x/CustomErrorListener.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include <antlr4-runtime.h>
#include <exception>
#include <string>


namespace cqasm::v3x::parser {

class CustomErrorListener : public antlr4::BaseErrorListener {
/**
* Name of the file being parsed.
*/
std::string file_name_;
private:
void syntaxError(antlr4::Recognizer *recognizer, antlr4::Token *offendingSymbol, size_t line,
size_t charPositionInLine, const std::string &msg, std::exception_ptr e) override;
public:
explicit CustomErrorListener(const std::string &file_name = "<unknown>")
: file_name_{ file_name } {}
};

} // namespace cqasm::v3x::parser
31 changes: 17 additions & 14 deletions include/v3x/cqasm-parse-helper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@

#include "cqasm-annotations.hpp"
#include "v1x/cqasm-parse-result.hpp"
#include "v3x/BuildCustomAstVisitor.h"
#include "v3x/BuildTreeGenAstVisitor.h"

#include "antlr4-runtime/antlr4-runtime.h"
#include "v3x/BuildCustomAstVisitor.hpp"
#include "v3x/CustomErrorListener.hpp"

#include <antlr4-runtime.h>
#include <fstream> // ifstream
#include <memory> // unique_ptr
#include <string>
Expand All @@ -28,33 +27,37 @@ using SourceLocation = annotations::SourceLocation;
struct ScannerAdaptor {
virtual ~ScannerAdaptor();

virtual void parse(const std::string &file_name, cqasm::v1x::parser::ParseResult &result) = 0;
virtual cqasm::v1x::parser::ParseResult parse() = 0;
};

class ScannerAntlr : public ScannerAdaptor {
std::unique_ptr<BuildCustomAstVisitor> build_visitor_up_;
std::unique_ptr<CustomErrorListener> error_listener_up_;
protected:
void parse_(antlr4::ANTLRInputStream &is, const std::string &file_name, cqasm::v1x::parser::ParseResult &result);
cqasm::v1x::parser::ParseResult parse_(antlr4::ANTLRInputStream &is);
public:
explicit ScannerAntlr(std::unique_ptr<BuildCustomAstVisitor> build_visitor_up);
explicit ScannerAntlr(std::unique_ptr<BuildCustomAstVisitor> build_visitor_up,
std::unique_ptr<CustomErrorListener> error_listener_up);
~ScannerAntlr() override;
void parse(const std::string &file_name, cqasm::v1x::parser::ParseResult &result) override = 0;
cqasm::v1x::parser::ParseResult parse() override = 0;
};

class ScannerAntlrFile : public ScannerAntlr {
std::ifstream ifs_;
std::string file_path_;
public:
ScannerAntlrFile(std::unique_ptr<BuildCustomAstVisitor> build_visitor_up, const std::string &file_path);
ScannerAntlrFile(std::unique_ptr<BuildCustomAstVisitor> build_visitor_up,
std::unique_ptr<CustomErrorListener> error_listener_up, const std::string &file_path);
~ScannerAntlrFile() override;
void parse(const std::string &file_name, cqasm::v1x::parser::ParseResult &result) override;
cqasm::v1x::parser::ParseResult parse() override;
};

class ScannerAntlrString : public ScannerAntlr {
std::string data_;
public:
ScannerAntlrString(std::unique_ptr<BuildCustomAstVisitor> build_visitor_up, const std::string &data);
ScannerAntlrString(std::unique_ptr<BuildCustomAstVisitor> build_visitor_up,
std::unique_ptr<CustomErrorListener> error_listener_up, const std::string &data);
~ScannerAntlrString() override;
void parse(const std::string &file_name, cqasm::v1x::parser::ParseResult &result) override;
cqasm::v1x::parser::ParseResult parse() override;
};


Expand Down Expand Up @@ -83,7 +86,7 @@ class ParseHelper {
/**
* Name of the file being parsed.
*/
std::string file_name;
std::string file_name_;

public:
explicit ParseHelper(std::unique_ptr<ScannerAdaptor> scanner_up, std::string file_name = "<unknown>");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
SUCCESS
Program(
version: <
Version(
items: 3
)
>
num_qubits: <
IntegerLiteral(
value: 3
)
>
statements: <
StatementList(
items: [
Bundle(
items: [
Instruction(
name: <
Identifier(
name: x
)
>
condition: -
operands: <
ExpressionList(
items: [
IntegerLiteral(
value: 1
)
]
)
>
annotations: []
)
]
annotations: []
)
]
)
>
)

Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
version 3
qubits 3
x 1
x q[3]
Empty file.
3 changes: 0 additions & 3 deletions res/v1x/toy-v1x-parsing/expression/qubits_3__x/input.cq

This file was deleted.

Empty file.
Empty file.
Empty file.
3 changes: 0 additions & 3 deletions res/v1x/toy-v1x-parsing/expression/qubits_3__x_3_14/input.cq

This file was deleted.

Empty file.
3 changes: 0 additions & 3 deletions res/v1x/toy-v1x-parsing/expression/qubits_3__x_v/input.cq

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ERROR
input.cq:5:11: extraneous input ',' expecting {INTEGER_LITERAL, FLOAT_LITERAL, IDENTIFIER}
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ version 3
qubits 3
h q[0]
h q[1]
cnot 123abc, q[0]
cnot 123abc, q[0]
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ERROR
input.cq:5:17: mismatched input '<EOF>' expecting {INTEGER_LITERAL, FLOAT_LITERAL, IDENTIFIER}
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ version 3
qubits 3
h q[0]
h q[1]
cnot q[0], 123abc
cnot q[0], 123abc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ERROR
input.cq:5:10: mismatched input '<EOF>' expecting {INTEGER_LITERAL, FLOAT_LITERAL, IDENTIFIER}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
version 3
qubits 3
h q[0];h q[1]
cnot q[0],
h q[0]
h q[1]
cnot q[0],
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
SUCCESS
Program(
version: <
Version(
items: 3
)
>
num_qubits: <
IntegerLiteral(
value: 3
)
>
statements: <
StatementList(
items: [
Bundle(
items: [
Instruction(
name: <
Identifier(
name: h
)
>
condition: -
operands: <
ExpressionList(
items: [
Index(
expr: <
Identifier(
name: q
)
>
indices: <
IndexList(
items: [
IndexItem(
index: <
IntegerLiteral(
value: 0
)
>
)
]
)
>
)
]
)
>
annotations: []
)
]
annotations: []
)
Bundle(
items: [
Instruction(
name: <
Identifier(
name: h
)
>
condition: -
operands: <
ExpressionList(
items: [
Index(
expr: <
Identifier(
name: q
)
>
indices: <
IndexList(
items: [
IndexItem(
index: <
IntegerLiteral(
value: 1
)
>
)
]
)
>
)
]
)
>
annotations: []
)
]
annotations: []
)
Bundle(
items: [
Instruction(
name: <
Identifier(
name: cnot
)
>
condition: -
operands: <
ExpressionList(
items: [
Index(
expr: <
Identifier(
name: q
)
>
indices: <
IndexList(
items: [
IndexItem(
index: <
IntegerLiteral(
value: 0
)
>
)
]
)
>
)
Index(
expr: <
Identifier(
name: q
)
>
indices: <
IndexList(
items: [
IndexItem(
index: <
IntegerLiteral(
value: 1
)
>
)
]
)
>
)
]
)
>
annotations: []
)
]
annotations: []
)
]
)
>
)

Loading

0 comments on commit 37eef7b

Please sign in to comment.