diff --git a/source/grammar/qasm3Lexer.g4 b/source/grammar/qasm3Lexer.g4 index 7daaeac..00a605f 100644 --- a/source/grammar/qasm3Lexer.g4 +++ b/source/grammar/qasm3Lexer.g4 @@ -36,9 +36,10 @@ IN: 'in'; SWITCH: 'switch'; CASE: 'case'; DEFAULT: 'default'; +NOP: 'nop'; PRAGMA: '#'? 'pragma' -> pushMode(EAT_TO_LINE_END); -AnnotationKeyword: '@' Identifier -> pushMode(EAT_TO_LINE_END); +AnnotationKeyword: '@' Identifier ('.' Identifier)* -> pushMode(EAT_TO_LINE_END); /* Types. */ @@ -121,7 +122,7 @@ TILDE: '~'; EXCLAMATION_POINT: '!'; EqualityOperator: '==' | '!='; -CompoundAssignmentOperator: '+=' | '-=' | '*=' | '/=' | '&=' | '|=' | '~=' | '^=' | '<<=' | '>>=' | '%=' | '**='; +CompoundAssignmentOperator: '+=' | '-=' | '*=' | '/=' | '&=' | '|=' | '^=' | '<<=' | '>>=' | '%=' | '**='; ComparisonOperator: '>' | '<' | '>=' | '<='; BitshiftOperator: '>>' | '<<'; @@ -159,8 +160,8 @@ BitstringLiteral: '"' ([01] '_'?)* [01] '"'; // Ignore whitespace between tokens, and define C++-style comments. Whitespace: [ \t]+ -> skip ; Newline: [\r\n]+ -> skip ; -LineComment : '//' ~[\r\n]* -> skip; -BlockComment : '/*' .*? '*/' -> skip; +LineComment : '//' ~[\r\n]* -> channel(HIDDEN); +BlockComment : '/*' .*? '*/' -> channel(HIDDEN); // The version identifier token would be ambiguous between itself and @@ -200,12 +201,12 @@ mode EAT_TO_LINE_END; // of the tokens, because ANTLR doesn't allow us to inherit rules directly. mode CAL_PRELUDE; CAL_PRELUDE_WHITESPACE: [ \t\r\n]+ -> skip; - CAL_PRELUDE_COMMENT: (LineComment | BlockComment) -> skip; + CAL_PRELUDE_COMMENT: (LineComment | BlockComment) -> channel(HIDDEN); CAL_PRELUDE_LBRACE: LBRACE -> type(LBRACE), mode(CAL_BLOCK); mode DEFCAL_PRELUDE; DEFCAL_PRELUDE_WHITESPACE: [ \t\r\n]+ -> skip; - DEFCAL_PRELUDE_COMMENT: (LineComment | BlockComment) -> skip; + DEFCAL_PRELUDE_COMMENT: (LineComment | BlockComment) -> channel(HIDDEN); DEFCAL_PRELUDE_LBRACE: LBRACE -> type(LBRACE), mode(CAL_BLOCK); // Duplications of valid constant expression tokens that may appear in the diff --git a/source/grammar/qasm3Parser.g4 b/source/grammar/qasm3Parser.g4 index 439845d..1fe3693 100644 --- a/source/grammar/qasm3Parser.g4 +++ b/source/grammar/qasm3Parser.g4 @@ -39,6 +39,7 @@ statement: | includeStatement | ioDeclarationStatement | measureArrowAssignmentStatement + | nopStatement | oldStyleDeclarationStatement | quantumDeclarationStatement | resetStatement @@ -66,7 +67,7 @@ continueStatement: CONTINUE SEMICOLON; endStatement: END SEMICOLON; forStatement: FOR scalarType Identifier IN (setExpression | LBRACKET rangeExpression RBRACKET | expression) body=statementOrScope; ifStatement: IF LPAREN expression RPAREN if_body=statementOrScope (ELSE else_body=statementOrScope)?; -returnStatement: RETURN (expression | measureExpression)? SEMICOLON; +returnStatement: RETURN (expression | measureExpression | quantumCallExpression)? SEMICOLON; whileStatement: WHILE LPAREN expression RPAREN body=statementOrScope; switchStatement: SWITCH LPAREN expression RPAREN LBRACE switchCaseItem* RBRACE; switchCaseItem: @@ -78,6 +79,7 @@ switchCaseItem: barrierStatement: BARRIER gateOperandList? SEMICOLON; boxStatement: BOX designator? scope; delayStatement: DELAY designator gateOperandList? SEMICOLON; +nopStatement: NOP gateOperandList? SEMICOLON; /* `gateCallStatement` is split in two to avoid a potential ambiguity with an * `expressionStatement` that consists of a single function call. The only * "gate" that can have no operands is `gphase` with no control modifiers, and @@ -92,7 +94,7 @@ gateCallStatement: ; // measureArrowAssignmentStatement also permits the case of not assigning the // result to any classical value too. -measureArrowAssignmentStatement: measureExpression (ARROW indexedIdentifier)? SEMICOLON; +measureArrowAssignmentStatement: (measureExpression | quantumCallExpression) (ARROW indexedIdentifier)? SEMICOLON; resetStatement: RESET gateOperand SEMICOLON; // Primitive declaration statements. @@ -109,7 +111,7 @@ externStatement: EXTERN Identifier LPAREN externArgumentList? RPAREN returnSigna gateStatement: GATE Identifier (LPAREN params=identifierList? RPAREN)? qubits=identifierList scope; // Non-declaration assignments and calculations. -assignmentStatement: indexedIdentifier op=(EQUALS | CompoundAssignmentOperator) (expression | measureExpression) SEMICOLON; +assignmentStatement: indexedIdentifier op=(EQUALS | CompoundAssignmentOperator) (expression | measureExpression | quantumCallExpression) SEMICOLON; expressionStatement: expression SEMICOLON; // Statements where the bulk is in the calibration language. @@ -160,11 +162,12 @@ expression: // Special-case expressions that are only valid in certain contexts. These are // not in the expression tree, but can contain elements that are within it. aliasExpression: expression (DOUBLE_PLUS expression)*; -declarationExpression: arrayLiteral | expression | measureExpression; +declarationExpression: arrayLiteral | expression | measureExpression | quantumCallExpression; measureExpression: MEASURE gateOperand; +quantumCallExpression: Identifier (LPAREN expressionList? RPAREN)? gateOperandList; rangeExpression: expression? COLON expression? (COLON expression)?; setExpression: LBRACE expression (COMMA expression)* COMMA? RBRACE; -arrayLiteral: LBRACE (expression | arrayLiteral) (COMMA (expression | arrayLiteral))* COMMA? RBRACE; +arrayLiteral: LBRACE ((expression | arrayLiteral) (COMMA (expression | arrayLiteral))* COMMA?)? RBRACE; // The general form is a comma-separated list of indexing entities. // `setExpression` is only valid when being used as a single index: registers