diff --git a/.github/workflows/cmake.yml b/.github/workflows/cmake.yml index f5f5e57..85bc4be 100644 --- a/.github/workflows/cmake.yml +++ b/.github/workflows/cmake.yml @@ -6,11 +6,13 @@ on: paths: - "ports/cpp/**" - ".github/workflows/*" + - "tests/*.g4" pull_request: branches: ["main"] paths: - "ports/cpp/**" - ".github/workflows/*" + - "tests/*.g4" jobs: build: diff --git a/ports/cpp/cmake/Testing.cmake b/ports/cpp/cmake/Testing.cmake index 8646464..b256131 100644 --- a/ports/cpp/cmake/Testing.cmake +++ b/ports/cpp/cmake/Testing.cmake @@ -4,8 +4,19 @@ macro(define_grammar_test grammar) ${CMAKE_CURRENT_LIST_DIR} NAME ) + set( + ANTLR4C3_TS_PROJECT_ROOT + ${CMAKE_CURRENT_LIST_DIR}/../../../.. + ) + + configure_file( + ${ANTLR4C3_TS_PROJECT_ROOT}/tests/${grammar} + ${CMAKE_CURRENT_BINARY_DIR}/${grammar} + COPYONLY + ) + antlr_generate( - ${CMAKE_CURRENT_LIST_DIR}/${grammar} + ${CMAKE_CURRENT_BINARY_DIR}/${grammar} ${CMAKE_CURRENT_BINARY_DIR} ) diff --git a/ports/cpp/test/cpp14/CPP14.g4 b/ports/cpp/test/cpp14/CPP14.g4 deleted file mode 100644 index ec8d0b0..0000000 --- a/ports/cpp/test/cpp14/CPP14.g4 +++ /dev/null @@ -1,1930 +0,0 @@ -/******************************************************************************* - * The MIT License (MIT) - * - * Copyright (c) 2015 Camilo Sanchez (Camiloasc1) - * - * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and - * associated documentation files (the "Software"), to deal in the Software without restriction, - * including without limitation the rights to use, copy, modify, merge, publish, distribute, - * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all copies or - * substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT - * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND - * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, - * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - * **************************************************************************** - */ - -/******************************************************************************* - * C++14 Grammar for ANTLR v4 - * - * Based on n4140 draft paper https://github.com/cplusplus/draft/blob/master/papers/n4140.pdf and - * http://www.nongnu.org/hcb/ - * - * Possible Issues: - * - * Input must avoid conditional compilation blocks (this grammar ignores any preprocessor directive) - * GCC extensions not yet supported (do not try to parse the preprocessor output) Right angle - * bracket (C++11) - Solution '>>' and '>>=' are not tokens, only '>' Lexer issue with - * pure-specifier rule ('0' token) - Solution in embedded code Change it to match the target - * language you want in line 1097 or verify inside your listeners/visitors Java: - * if($val.text.compareTo("0")!=0) throw new InputMismatchException(this); JavaScript: - * - * Python2: - * - * Python3: - * - * C#: - * - * **************************************************************************** - */ -grammar CPP14; - -// $antlr-format columnLimit 100, minEmptyLines 1, maxEmptyLinesToKeep 1, useTab false -// $antlr-format reflowComments false, breakBeforeBraces false -// $antlr-format keepEmptyLinesAtTheStartOfBlocks false, allowShortRulesOnASingleLine false -// $antlr-format alignSemicolons hanging, alignColons hanging, alignTrailingComments true - -/*Basic concepts*/ -translationunit - : declarationseq? EOF - ; - -/*Expressions*/ -primaryexpression - : literal - | This - | '(' expression ')' - | idexpression - | lambdaexpression - ; - -idexpression - : unqualifiedid - | qualifiedid - ; - -unqualifiedid - : Identifier - | operatorfunctionid - | conversionfunctionid - | literaloperatorid - | '~' classname - | '~' decltypespecifier - | templateid - ; - -qualifiedid - : nestednamespecifier Template? unqualifiedid - ; - -nestednamespecifier - : '::' - | typename '::' - | namespacename '::' - | decltypespecifier '::' - | nestednamespecifier Identifier '::' - | nestednamespecifier Template? simpletemplateid '::' - ; - -lambdaexpression - : lambdaintroducer lambdadeclarator? compoundstatement - ; - -lambdaintroducer - : '[' lambdacapture? ']' - ; - -lambdacapture - : capturedefault - | capturelist - | capturedefault ',' capturelist - ; - -capturedefault - : '&' - | '=' - ; - -capturelist - : capture '...'? - | capturelist ',' capture '...'? - ; - -capture - : simplecapture - | initcapture - ; - -simplecapture - : Identifier - | '&' Identifier - | This - ; - -initcapture - : Identifier initializer - | '&' Identifier initializer - ; - -lambdadeclarator - : '(' parameterdeclarationclause ')' Mutable? exceptionspecification? attributespecifierseq? - trailingreturntype? - ; - -postfixexpression - : primaryexpression - | postfixexpression '[' expression ']' - | postfixexpression '[' bracedinitlist ']' - | postfixexpression '(' expressionlist? ')' - | simpletypespecifier '(' expressionlist? ')' - | typenamespecifier '(' expressionlist? ')' - | simpletypespecifier bracedinitlist - | typenamespecifier bracedinitlist - | postfixexpression '.' Template? idexpression - | postfixexpression '->' Template? idexpression - | postfixexpression '.' pseudodestructorname - | postfixexpression '->' pseudodestructorname - | postfixexpression '++' - | postfixexpression '--' - | Dynamic_cast '<' typeid '>' '(' expression ')' - | Static_cast '<' typeid '>' '(' expression ')' - | Reinterpret_cast '<' typeid '>' '(' expression ')' - | Const_cast '<' typeid '>' '(' expression ')' - | Typeid '(' expression ')' - | Typeid '(' typeid ')' - ; - -expressionlist - : initializerlist - ; - -pseudodestructorname - : nestednamespecifier? typename '::' '~' typename - | nestednamespecifier Template simpletemplateid '::' '~' typename - | nestednamespecifier? '~' typename - | '~' decltypespecifier - ; - -unaryexpression - : postfixexpression - | '++' castexpression - | '--' castexpression - | unaryoperator castexpression - | Sizeof unaryexpression - | Sizeof '(' typeid ')' - | Sizeof '...' '(' Identifier ')' - | Alignof '(' typeid ')' - | noexceptexpression - | newexpression - | deleteexpression - ; - -unaryoperator - : '|' - | '*' - | '&' - | '+' - | '!' - | '~' - | '-' - ; - -newexpression - : '::'? New newplacement? newtypeid newinitializer? - | '::'? New newplacement? '(' typeid ')' newinitializer? - ; - -newplacement - : '(' expressionlist ')' - ; - -newtypeid - : typespecifierseq newdeclarator? - ; - -newdeclarator - : ptroperator newdeclarator? - | noptrnewdeclarator - ; - -noptrnewdeclarator - : '[' expression ']' attributespecifierseq? - | noptrnewdeclarator '[' constantexpression ']' attributespecifierseq? - ; - -newinitializer - : '(' expressionlist? ')' - | bracedinitlist - ; - -deleteexpression - : '::'? Delete castexpression - | '::'? Delete '[' ']' castexpression - ; - -noexceptexpression - : Noexcept '(' expression ')' - ; - -castexpression - : unaryexpression - | '(' typeid ')' castexpression - ; - -pmexpression - : castexpression - | pmexpression '.*' castexpression - | pmexpression '->*' castexpression - ; - -multiplicativeexpression - : pmexpression - | multiplicativeexpression '*' pmexpression - | multiplicativeexpression '/' pmexpression - | multiplicativeexpression '%' pmexpression - ; - -additiveexpression - : multiplicativeexpression - | additiveexpression '+' multiplicativeexpression - | additiveexpression '-' multiplicativeexpression - ; - -shiftexpression - : additiveexpression - | shiftexpression '<<' additiveexpression - | shiftexpression rightShift additiveexpression - ; - -relationalexpression - : shiftexpression - | relationalexpression '<' shiftexpression - | relationalexpression '>' shiftexpression - | relationalexpression '<=' shiftexpression - | relationalexpression '>=' shiftexpression - ; - -equalityexpression - : relationalexpression - | equalityexpression '==' relationalexpression - | equalityexpression '!=' relationalexpression - ; - -andexpression - : equalityexpression - | andexpression '&' equalityexpression - ; - -exclusiveorexpression - : andexpression - | exclusiveorexpression '^' andexpression - ; - -inclusiveorexpression - : exclusiveorexpression - | inclusiveorexpression '|' exclusiveorexpression - ; - -logicalandexpression - : inclusiveorexpression - | logicalandexpression '&&' inclusiveorexpression - ; - -logicalorexpression - : logicalandexpression - | logicalorexpression '||' logicalandexpression - ; - -conditionalexpression - : logicalorexpression - | logicalorexpression '?' expression ':' assignmentexpression - ; - -assignmentexpression - : conditionalexpression - | logicalorexpression assignmentoperator initializerclause - | throwexpression - ; - -assignmentoperator - : '=' - | '*=' - | '/=' - | '%=' - | '+=' - | '-=' - | rightShiftAssign - | '<<=' - | '&=' - | '^=' - | '|=' - ; - -expression - : assignmentexpression - | expression ',' assignmentexpression - ; - -constantexpression - : conditionalexpression - ; - -/*Statements*/ -statement - : labeledstatement - | attributespecifierseq? expressionstatement - | attributespecifierseq? compoundstatement - | attributespecifierseq? selectionstatement - | attributespecifierseq? iterationstatement - | attributespecifierseq? jumpstatement - | declarationstatement - | attributespecifierseq? tryblock - ; - -labeledstatement - : attributespecifierseq? Identifier ':' statement - | attributespecifierseq? Case constantexpression ':' statement - | attributespecifierseq? Default ':' statement - ; - -expressionstatement - : expression? ';' - ; - -compoundstatement - : '{' statementseq? '}' - ; - -statementseq - : statement - | statementseq statement - ; - -selectionstatement - : If '(' condition ')' statement - | If '(' condition ')' statement Else statement - | Switch '(' condition ')' statement - ; - -condition - : expression - | attributespecifierseq? declspecifierseq declarator '=' initializerclause - | attributespecifierseq? declspecifierseq declarator bracedinitlist - ; - -iterationstatement - : While '(' condition ')' statement - | Do statement While '(' expression ')' ';' - | For '(' forinitstatement condition? ';' expression? ')' statement - | For '(' forrangedeclaration ':' forrangeinitializer ')' statement - ; - -forinitstatement - : expressionstatement - | simpledeclaration - ; - -forrangedeclaration - : attributespecifierseq? declspecifierseq declarator - ; - -forrangeinitializer - : expression - | bracedinitlist - ; - -jumpstatement - : Break ';' - | Continue ';' - | Return expression? ';' - | Return bracedinitlist ';' - | Goto Identifier ';' - ; - -declarationstatement - : blockdeclaration - ; - -/*Declarations*/ -declarationseq - : declaration - | declarationseq declaration - ; - -declaration - : blockdeclaration - | functiondefinition - | templatedeclaration - | explicitinstantiation - | explicitspecialization - | linkagespecification - | namespacedefinition - | emptydeclaration - | attributedeclaration - ; - -blockdeclaration - : simpledeclaration - | asmdefinition - | namespacealiasdefinition - | usingdeclaration - | usingdirective - | static_assertdeclaration - | aliasdeclaration - | opaqueenumdeclaration - ; - -aliasdeclaration - : Using Identifier attributespecifierseq? '=' typeid ';' - ; - -simpledeclaration - : declspecifierseq? initdeclaratorlist? ';' - | attributespecifierseq declspecifierseq? initdeclaratorlist ';' - ; - -static_assertdeclaration - : Static_assert '(' constantexpression ',' Stringliteral ')' ';' - ; - -emptydeclaration - : ';' - ; - -attributedeclaration - : attributespecifierseq ';' - ; - -declspecifier - : storageclassspecifier - | typespecifier - | functionspecifier - | Friend - | Typedef - | Constexpr - ; - -declspecifierseq - : declspecifier attributespecifierseq? - | declspecifier declspecifierseq - ; - -storageclassspecifier - : Register - | Static - | Thread_local - | Extern - | Mutable - ; - -functionspecifier - : Inline - | Virtual - | Explicit - ; - -typedefname - : Identifier - ; - -typespecifier - : trailingtypespecifier - | classspecifier - | enumspecifier - ; - -trailingtypespecifier - : simpletypespecifier - | elaboratedtypespecifier - | typenamespecifier - | cvqualifier - ; - -typespecifierseq - : typespecifier attributespecifierseq? - | typespecifier typespecifierseq - ; - -trailingtypespecifierseq - : trailingtypespecifier attributespecifierseq? - | trailingtypespecifier trailingtypespecifierseq - ; - -simpletypespecifier - : nestednamespecifier? typename - | nestednamespecifier Template simpletemplateid - | Char - | Char16 - | Char32 - | Wchar - | Bool - | Short - | Int - | Long - | Signed - | Unsigned - | Float - | Double - | Void - | Auto - | decltypespecifier - ; - -typename - : classname - | enumname - | typedefname - | simpletemplateid - ; - -decltypespecifier - : Decltype '(' expression ')' - | Decltype '(' Auto ')' - ; - -elaboratedtypespecifier - : classkey attributespecifierseq? nestednamespecifier? Identifier - | classkey simpletemplateid - | classkey nestednamespecifier Template? simpletemplateid - | Enum nestednamespecifier? Identifier - ; - -enumname - : Identifier - ; - -enumspecifier - : enumhead '{' enumeratorlist? '}' - | enumhead '{' enumeratorlist ',' '}' - ; - -enumhead - : enumkey attributespecifierseq? Identifier? enumbase? - | enumkey attributespecifierseq? nestednamespecifier Identifier enumbase? - ; - -opaqueenumdeclaration - : enumkey attributespecifierseq? Identifier enumbase? ';' - ; - -enumkey - : Enum - | Enum Class - | Enum Struct - ; - -enumbase - : ':' typespecifierseq - ; - -enumeratorlist - : enumeratordefinition - | enumeratorlist ',' enumeratordefinition - ; - -enumeratordefinition - : enumerator - | enumerator '=' constantexpression - ; - -enumerator - : Identifier - ; - -namespacename - : originalnamespacename - | namespacealias - ; - -originalnamespacename - : Identifier - ; - -namespacedefinition - : namednamespacedefinition - | unnamednamespacedefinition - ; - -namednamespacedefinition - : originalnamespacedefinition - | extensionnamespacedefinition - ; - -originalnamespacedefinition - : Inline? Namespace Identifier '{' namespacebody '}' - ; - -extensionnamespacedefinition - : Inline? Namespace originalnamespacename '{' namespacebody '}' - ; - -unnamednamespacedefinition - : Inline? Namespace '{' namespacebody '}' - ; - -namespacebody - : declarationseq? - ; - -namespacealias - : Identifier - ; - -namespacealiasdefinition - : Namespace Identifier '=' qualifiednamespacespecifier ';' - ; - -qualifiednamespacespecifier - : nestednamespecifier? namespacename - ; - -usingdeclaration - : Using Typename? nestednamespecifier unqualifiedid ';' - | Using '::' unqualifiedid ';' - ; - -usingdirective - : attributespecifierseq? Using Namespace nestednamespecifier? namespacename ';' - ; - -asmdefinition - : Asm '(' Stringliteral ')' ';' - ; - -linkagespecification - : Extern Stringliteral '{' declarationseq? '}' - | Extern Stringliteral declaration - ; - -attributespecifierseq - : attributespecifier - | attributespecifierseq attributespecifier - ; - -attributespecifier - : '[' '[' attributelist ']' ']' - | alignmentspecifier - ; - -alignmentspecifier - : Alignas '(' typeid '...'? ')' - | Alignas '(' constantexpression '...'? ')' - ; - -attributelist - : attribute? - | attributelist ',' attribute? - | attribute '...' - | attributelist ',' attribute '...' - ; - -attribute - : attributetoken attributeargumentclause? - ; - -attributetoken - : Identifier - | attributescopedtoken - ; - -attributescopedtoken - : attributenamespace '::' Identifier - ; - -attributenamespace - : Identifier - ; - -attributeargumentclause - : '(' balancedtokenseq ')' - ; - -balancedtokenseq - : balancedtoken? - | balancedtokenseq balancedtoken - ; - -balancedtoken - : '(' balancedtokenseq ')' - | '[' balancedtokenseq ']' - | '{' balancedtokenseq '}' - /*any token other than a parenthesis , a bracket , or a brace*/ - ; - -/*Declarators*/ -initdeclaratorlist - : initdeclarator - | initdeclaratorlist ',' initdeclarator - ; - -initdeclarator - : declarator initializer? - ; - -declarator - : ptrdeclarator - | noptrdeclarator parametersandqualifiers trailingreturntype - ; - -ptrdeclarator - : noptrdeclarator - | ptroperator ptrdeclarator - ; - -noptrdeclarator - : declaratorid attributespecifierseq? - | noptrdeclarator parametersandqualifiers - | noptrdeclarator '[' constantexpression? ']' attributespecifierseq? - | '(' ptrdeclarator ')' - ; - -parametersandqualifiers - : '(' parameterdeclarationclause ')' cvqualifierseq? refqualifier? exceptionspecification? - attributespecifierseq? - ; - -trailingreturntype - : '->' trailingtypespecifierseq abstractdeclarator? - ; - -ptroperator - : '*' attributespecifierseq? cvqualifierseq? - | '&' attributespecifierseq? - | '&&' attributespecifierseq? - | nestednamespecifier '*' attributespecifierseq? cvqualifierseq? - ; - -cvqualifierseq - : cvqualifier cvqualifierseq? - ; - -cvqualifier - : Const - | Volatile - ; - -refqualifier - : '&' - | '&&' - ; - -declaratorid - : '...'? idexpression - ; - -typeid - : typespecifierseq abstractdeclarator? - ; - -abstractdeclarator - : ptrabstractdeclarator - | noptrabstractdeclarator? parametersandqualifiers trailingreturntype - | abstractpackdeclarator - ; - -ptrabstractdeclarator - : noptrabstractdeclarator - | ptroperator ptrabstractdeclarator? - ; - -noptrabstractdeclarator - : noptrabstractdeclarator parametersandqualifiers - | parametersandqualifiers - | noptrabstractdeclarator '[' constantexpression? ']' attributespecifierseq? - | '[' constantexpression? ']' attributespecifierseq? - | '(' ptrabstractdeclarator ')' - ; - -abstractpackdeclarator - : noptrabstractpackdeclarator - | ptroperator abstractpackdeclarator - ; - -noptrabstractpackdeclarator - : noptrabstractpackdeclarator parametersandqualifiers - | noptrabstractpackdeclarator '[' constantexpression? ']' attributespecifierseq? - | '...' - ; - -parameterdeclarationclause - : parameterdeclarationlist? '...'? - | parameterdeclarationlist ',' '...' - ; - -parameterdeclarationlist - : parameterdeclaration - | parameterdeclarationlist ',' parameterdeclaration - ; - -parameterdeclaration - : attributespecifierseq? declspecifierseq declarator - | attributespecifierseq? declspecifierseq declarator '=' initializerclause - | attributespecifierseq? declspecifierseq abstractdeclarator? - | attributespecifierseq? declspecifierseq abstractdeclarator? '=' initializerclause - ; - -functiondefinition - : attributespecifierseq? declspecifierseq? declarator virtspecifierseq? functionbody - ; - -functionbody - : ctorinitializer? compoundstatement - | functiontryblock - | '=' Default ';' - | '=' Delete ';' - ; - -initializer - : braceorequalinitializer - | '(' expressionlist ')' - ; - -braceorequalinitializer - : '=' initializerclause - | bracedinitlist - ; - -initializerclause - : assignmentexpression - | bracedinitlist - ; - -initializerlist - : initializerclause '...'? - | initializerlist ',' initializerclause '...'? - ; - -bracedinitlist - : '{' initializerlist ','? '}' - | '{' '}' - ; - -/*Classes*/ -classname - : Identifier - | simpletemplateid - ; - -classspecifier - : classhead '{' memberspecification? '}' - ; - -classhead - : classkey attributespecifierseq? classheadname classvirtspecifier? baseclause? - | classkey attributespecifierseq? baseclause? - ; - -classheadname - : nestednamespecifier? classname - ; - -classvirtspecifier - : Final - ; - -classkey - : Class - | Struct - | Union - ; - -memberspecification - : memberdeclaration memberspecification? - | accessspecifier ':' memberspecification? - ; - -memberdeclaration - : attributespecifierseq? declspecifierseq? memberdeclaratorlist? ';' - | functiondefinition - | usingdeclaration - | static_assertdeclaration - | templatedeclaration - | aliasdeclaration - | emptydeclaration - ; - -memberdeclaratorlist - : memberdeclarator - | memberdeclaratorlist ',' memberdeclarator - ; - -memberdeclarator - : declarator virtspecifierseq? purespecifier? - | declarator braceorequalinitializer? - | Identifier? attributespecifierseq? ':' constantexpression - ; - -virtspecifierseq - : virtspecifier - | virtspecifierseq virtspecifier - ; - -virtspecifier - : Override - | Final - ; - -/* -purespecifier: - '=' '0'//Conflicts with the lexer - ; - */ -purespecifier - : Assign val = Octalliteral { if ($val.text != "0") throw InputMismatchException(this); } - ; - -/*Derived classes*/ -baseclause - : ':' basespecifierlist - ; - -basespecifierlist - : basespecifier '...'? - | basespecifierlist ',' basespecifier '...'? - ; - -basespecifier - : attributespecifierseq? basetypespecifier - | attributespecifierseq? Virtual accessspecifier? basetypespecifier - | attributespecifierseq? accessspecifier Virtual? basetypespecifier - ; - -classordecltype - : nestednamespecifier? classname - | decltypespecifier - ; - -basetypespecifier - : classordecltype - ; - -accessspecifier - : Private - | Protected - | Public - ; - -/*Special member functions*/ -conversionfunctionid - : Operator conversiontypeid - ; - -conversiontypeid - : typespecifierseq conversiondeclarator? - ; - -conversiondeclarator - : ptroperator conversiondeclarator? - ; - -ctorinitializer - : ':' meminitializerlist - ; - -meminitializerlist - : meminitializer '...'? - | meminitializer '...'? ',' meminitializerlist - ; - -meminitializer - : meminitializerid '(' expressionlist? ')' - | meminitializerid bracedinitlist - ; - -meminitializerid - : classordecltype - | Identifier - ; - -/*Overloading*/ -operatorfunctionid - : Operator operator - ; - -literaloperatorid - : Operator Stringliteral Identifier - | Operator Userdefinedstringliteral - ; - -/*Templates*/ -templatedeclaration - : Template '<' templateparameterlist '>' declaration - ; - -templateparameterlist - : templateparameter - | templateparameterlist ',' templateparameter - ; - -templateparameter - : typeparameter - | parameterdeclaration - ; - -typeparameter - : Class '...'? Identifier? - | Class Identifier? '=' typeid - | Typename '...'? Identifier? - | Typename Identifier? '=' typeid - | Template '<' templateparameterlist '>' Class '...'? Identifier? - | Template '<' templateparameterlist '>' Class Identifier? '=' idexpression - ; - -simpletemplateid - : templatename '<' templateargumentlist? '>' - ; - -templateid - : simpletemplateid - | operatorfunctionid '<' templateargumentlist? '>' - | literaloperatorid '<' templateargumentlist? '>' - ; - -templatename - : Identifier - ; - -templateargumentlist - : templateargument '...'? - | templateargumentlist ',' templateargument '...'? - ; - -templateargument - : typeid - | constantexpression - | idexpression - ; - -typenamespecifier - : Typename nestednamespecifier Identifier - | Typename nestednamespecifier Template? simpletemplateid - ; - -explicitinstantiation - : Extern? Template declaration - ; - -explicitspecialization - : Template '<' '>' declaration - ; - -/*Exception handling*/ -tryblock - : Try compoundstatement handlerseq - ; - -functiontryblock - : Try ctorinitializer? compoundstatement handlerseq - ; - -handlerseq - : handler handlerseq? - ; - -handler - : Catch '(' exceptiondeclaration ')' compoundstatement - ; - -exceptiondeclaration - : attributespecifierseq? typespecifierseq declarator - | attributespecifierseq? typespecifierseq abstractdeclarator? - | '...' - ; - -throwexpression - : Throw assignmentexpression? - ; - -exceptionspecification - : dynamicexceptionspecification - | noexceptspecification - ; - -dynamicexceptionspecification - : Throw '(' typeidlist? ')' - ; - -typeidlist - : typeid '...'? - | typeidlist ',' typeid '...'? - ; - -noexceptspecification - : Noexcept '(' constantexpression ')' - | Noexcept - ; - -/*Preprocessing directives*/ - -MultiLineMacro - : '#' (~[\n]*? '\\' '\r'? '\n')+ ~[\n]+ -> channel(HIDDEN) - ; - -Directive - : '#' ~[\n]* -> channel(HIDDEN) - ; - -/*Lexer*/ - -/*Keywords*/ -Alignas - : 'alignas' - ; - -Alignof - : 'alignof' - ; - -Asm - : 'asm' - ; - -Auto - : 'auto' - ; - -Bool - : 'bool' - ; - -Break - : 'break' - ; - -Case - : 'case' - ; - -Catch - : 'catch' - ; - -Char - : 'char' - ; - -Char16 - : 'char16_t' - ; - -Char32 - : 'char32_t' - ; - -Class - : 'class' - ; - -Const - : 'const' - ; - -Constexpr - : 'constexpr' - ; - -Const_cast - : 'const_cast' - ; - -Continue - : 'continue' - ; - -Decltype - : 'decltype' - ; - -Default - : 'default' - ; - -Delete - : 'delete' - ; - -Do - : 'do' - ; - -Double - : 'double' - ; - -Dynamic_cast - : 'dynamic_cast' - ; - -Else - : 'else' - ; - -Enum - : 'enum' - ; - -Explicit - : 'explicit' - ; - -Export - : 'export' - ; - -Extern - : 'extern' - ; - -False - : 'false' - ; - -Final - : 'final' - ; - -Float - : 'float' - ; - -For - : 'for' - ; - -Friend - : 'friend' - ; - -Goto - : 'goto' - ; - -If - : 'if' - ; - -Inline - : 'inline' - ; - -Int - : 'int' - ; - -Long - : 'long' - ; - -Mutable - : 'mutable' - ; - -Namespace - : 'namespace' - ; - -New - : 'new' - ; - -Noexcept - : 'noexcept' - ; - -Nullptr - : 'nullptr' - ; - -Operator - : 'operator' - ; - -Override - : 'override' - ; - -Private - : 'private' - ; - -Protected - : 'protected' - ; - -Public - : 'public' - ; - -Register - : 'register' - ; - -Reinterpret_cast - : 'reinterpret_cast' - ; - -Return - : 'return' - ; - -Short - : 'short' - ; - -Signed - : 'signed' - ; - -Sizeof - : 'sizeof' - ; - -Static - : 'static' - ; - -Static_assert - : 'static_assert' - ; - -Static_cast - : 'static_cast' - ; - -Struct - : 'struct' - ; - -Switch - : 'switch' - ; - -Template - : 'template' - ; - -This - : 'this' - ; - -Thread_local - : 'thread_local' - ; - -Throw - : 'throw' - ; - -True - : 'true' - ; - -Try - : 'try' - ; - -Typedef - : 'typedef' - ; - -Typeid - : 'typeid' - ; - -Typename - : 'typename' - ; - -Union - : 'union' - ; - -Unsigned - : 'unsigned' - ; - -Using - : 'using' - ; - -Virtual - : 'virtual' - ; - -Void - : 'void' - ; - -Volatile - : 'volatile' - ; - -Wchar - : 'wchar_t' - ; - -While - : 'while' - ; - -/*Operators*/ -LeftParen - : '(' - ; - -RightParen - : ')' - ; - -LeftBracket - : '[' - ; - -RightBracket - : ']' - ; - -LeftBrace - : '{' - ; - -RightBrace - : '}' - ; - -Plus - : '+' - ; - -Minus - : '-' - ; - -Star - : '*' - ; - -Div - : '/' - ; - -Mod - : '%' - ; - -Caret - : '^' - ; - -And - : '&' - ; - -Or - : '|' - ; - -Tilde - : '~' - ; - -Not - : '!' - ; - -Assign - : '=' - ; - -Less - : '<' - ; - -Greater - : '>' - ; - -PlusAssign - : '+=' - ; - -MinusAssign - : '-=' - ; - -StarAssign - : '*=' - ; - -DivAssign - : '/=' - ; - -ModAssign - : '%=' - ; - -XorAssign - : '^=' - ; - -AndAssign - : '&=' - ; - -OrAssign - : '|=' - ; - -LeftShift - : '<<' - ; - -rightShift - : - //'>>' - Greater Greater - ; - -LeftShiftAssign - : '<<=' - ; - -rightShiftAssign - : - //'>>=' - Greater Greater Assign - ; - -Equal - : '==' - ; - -NotEqual - : '!=' - ; - -LessEqual - : '<=' - ; - -GreaterEqual - : '>=' - ; - -AndAnd - : '&&' - ; - -OrOr - : '||' - ; - -PlusPlus - : '++' - ; - -MinusMinus - : '--' - ; - -Comma - : ',' - ; - -ArrowStar - : '->*' - ; - -Arrow - : '->' - ; - -Question - : '?' - ; - -Colon - : ':' - ; - -Doublecolon - : '::' - ; - -Semi - : ';' - ; - -Dot - : '.' - ; - -DotStar - : '.*' - ; - -Ellipsis - : '...' - ; - -operator - : New - | Delete - | New '[' ']' - | Delete '[' ']' - | '+' - | '-' - | '*' - | '/' - | '%' - | '^' - | '&' - | '|' - | '~' - | '!' - | '=' - | '<' - | '>' - | '+=' - | '-=' - | '*=' - | '/=' - | '%=' - | '^=' - | '&=' - | '|=' - | '<<' - | rightShift - | rightShiftAssign - | '<<=' - | '==' - | '!=' - | '<=' - | '>=' - | '&&' - | '||' - | '++' - | '--' - | ',' - | '->*' - | '->' - | '(' ')' - | '[' ']' - ; - -/*Lexer*/ -fragment Hexquad - : HEXADECIMALDIGIT HEXADECIMALDIGIT HEXADECIMALDIGIT HEXADECIMALDIGIT - ; - -fragment Universalcharactername - : '\\u' Hexquad - | '\\U' Hexquad Hexquad - ; - -Identifier - : - /* - Identifiernondigit - | Identifier Identifiernondigit - | Identifier DIGIT - */ Identifiernondigit (Identifiernondigit | DIGIT)* - ; - -fragment Identifiernondigit - : NONDIGIT - | Universalcharactername - /* other implementation defined characters*/ - ; - -fragment NONDIGIT - : [a-zA-Z_] - ; - -fragment DIGIT - : [0-9] - ; - -literal - : Integerliteral - | Characterliteral - | Floatingliteral - | Stringliteral - | booleanliteral - | pointerliteral - | userdefinedliteral - ; - -Integerliteral - : Decimalliteral Integersuffix? - | Octalliteral Integersuffix? - | Hexadecimalliteral Integersuffix? - | Binaryliteral Integersuffix? - ; - -Decimalliteral - : NONZERODIGIT ('\''? DIGIT)* - ; - -Octalliteral - : '0' ('\''? OCTALDIGIT)* - ; - -Hexadecimalliteral - : ('0x' | '0X') HEXADECIMALDIGIT ('\''? HEXADECIMALDIGIT)* - ; - -Binaryliteral - : ('0b' | '0B') BINARYDIGIT ('\''? BINARYDIGIT)* - ; - -fragment NONZERODIGIT - : [1-9] - ; - -fragment OCTALDIGIT - : [0-7] - ; - -fragment HEXADECIMALDIGIT - : [0-9a-fA-F] - ; - -fragment BINARYDIGIT - : [01] - ; - -Integersuffix - : Unsignedsuffix Longsuffix? - | Unsignedsuffix Longlongsuffix? - | Longsuffix Unsignedsuffix? - | Longlongsuffix Unsignedsuffix? - ; - -fragment Unsignedsuffix - : [uU] - ; - -fragment Longsuffix - : [lL] - ; - -fragment Longlongsuffix - : 'll' - | 'LL' - ; - -Characterliteral - : '\'' Cchar+ '\'' - | 'u' '\'' Cchar+ '\'' - | 'U' '\'' Cchar+ '\'' - | 'L' '\'' Cchar+ '\'' - ; - -fragment Cchar - : ~['\\\r\n] - | Escapesequence - | Universalcharactername - ; - -fragment Escapesequence - : Simpleescapesequence - | Octalescapesequence - | Hexadecimalescapesequence - ; - -fragment Simpleescapesequence - : '\\\'' - | '\\"' - | '\\?' - | '\\\\' - | '\\a' - | '\\b' - | '\\f' - | '\\n' - | '\\r' - | '\\t' - | '\\v' - ; - -fragment Octalescapesequence - : '\\' OCTALDIGIT - | '\\' OCTALDIGIT OCTALDIGIT - | '\\' OCTALDIGIT OCTALDIGIT OCTALDIGIT - ; - -fragment Hexadecimalescapesequence - : '\\x' HEXADECIMALDIGIT+ - ; - -Floatingliteral - : Fractionalconstant Exponentpart? Floatingsuffix? - | Digitsequence Exponentpart Floatingsuffix? - ; - -fragment Fractionalconstant - : Digitsequence? '.' Digitsequence - | Digitsequence '.' - ; - -fragment Exponentpart - : 'e' SIGN? Digitsequence - | 'E' SIGN? Digitsequence - ; - -fragment SIGN - : [+-] - ; - -fragment Digitsequence - : DIGIT ('\''? DIGIT)* - ; - -fragment Floatingsuffix - : [flFL] - ; - -Stringliteral - : Encodingprefix? '"' Schar* '"' - | Encodingprefix? 'R' Rawstring - ; - -fragment Encodingprefix - : 'u8' - | 'u' - | 'U' - | 'L' - ; - -fragment Schar - : ~["\\\r\n] - | Escapesequence - | Universalcharactername - ; - -fragment Rawstring /* '"' dcharsequence? '(' rcharsequence? ')' dcharsequence? '"' */ - : '"' .*? '(' .*? ')' .*? '"' - ; - -booleanliteral - : False - | True - ; - -pointerliteral - : Nullptr - ; - -userdefinedliteral - : Userdefinedintegerliteral - | Userdefinedfloatingliteral - | Userdefinedstringliteral - | Userdefinedcharacterliteral - ; - -Userdefinedintegerliteral - : Decimalliteral Udsuffix - | Octalliteral Udsuffix - | Hexadecimalliteral Udsuffix - | Binaryliteral Udsuffix - ; - -Userdefinedfloatingliteral - : Fractionalconstant Exponentpart? Udsuffix - | Digitsequence Exponentpart Udsuffix - ; - -Userdefinedstringliteral - : Stringliteral Udsuffix - ; - -Userdefinedcharacterliteral - : Characterliteral Udsuffix - ; - -fragment Udsuffix - : Identifier - ; - -Whitespace - : [ \t]+ -> skip - ; - -Newline - : ('\r' '\n'? | '\n') -> skip - ; - -BlockComment - : '/*' .*? '*/' -> skip - ; - -LineComment - : '//' ~[\r\n]* -> skip - ; diff --git a/ports/cpp/test/expr/Expr.g4 b/ports/cpp/test/expr/Expr.g4 deleted file mode 100644 index 4dcbc5b..0000000 --- a/ports/cpp/test/expr/Expr.g4 +++ /dev/null @@ -1,74 +0,0 @@ -grammar Expr; - -// $antlr-format columnLimit 100, minEmptyLines 1, maxEmptyLinesToKeep 1, useTab false -// $antlr-format reflowComments false, breakBeforeBraces false -// $antlr-format keepEmptyLinesAtTheStartOfBlocks false, allowShortRulesOnASingleLine false -// $antlr-format alignSemicolons hanging, alignColons hanging, alignTrailingComments true - -expression - : assignment - | simpleExpression - ; - -assignment - : (VAR | LET) ID EQUAL simpleExpression - ; - -simpleExpression - : simpleExpression (PLUS | MINUS) simpleExpression - | simpleExpression (MULTIPLY | DIVIDE) simpleExpression - | variableRef - | functionRef - ; - -variableRef - : ID - ; - -functionRef - : ID OPEN_PAR CLOSE_PAR - ; - -VAR - : [vV] [aA] [rR] - ; - -LET - : [lL] [eE] [tT] - ; - -PLUS - : '+' - ; - -MINUS - : '-' - ; - -MULTIPLY - : '*' - ; - -DIVIDE - : '/' - ; - -EQUAL - : '=' - ; - -OPEN_PAR - : '(' - ; - -CLOSE_PAR - : ')' - ; - -ID - : [a-zA-Z] [a-zA-Z0-9_]* - ; - -WS - : [ \n\r\t] -> channel(HIDDEN) - ; diff --git a/ports/cpp/test/whitebox/Whitebox.g4 b/ports/cpp/test/whitebox/Whitebox.g4 deleted file mode 100644 index 4acb400..0000000 --- a/ports/cpp/test/whitebox/Whitebox.g4 +++ /dev/null @@ -1,138 +0,0 @@ -grammar Whitebox; - -// $antlr-format columnLimit 100, minEmptyLines 1, maxEmptyLinesToKeep 1, useTab false -// $antlr-format reflowComments false, breakBeforeBraces false -// $antlr-format keepEmptyLinesAtTheStartOfBlocks false, allowShortRulesOnASingleLine false -// $antlr-format alignSemicolons hanging, alignColons hanging, alignTrailingComments true - -test1 - : rule1 ADIPISCING - ; - -rule1 - : rule2 CONSECTETUR - ; - -rule2 - : LOREM rule3 rule5 SIT* AMET? - ; - -rule3 - : rule4 DOLOR? - ; - -rule4 - : IPSUM? - ; - -rule5 - : - ; - -test2 - : rule7 ADIPISCING - ; - -rule7 - : rule8 CONSECTETUR - ; - -rule8 - : LOREM rule11 rule9 SIT* AMET? - ; - -rule9 - : rule10 DOLOR? - ; - -rule10 - : IPSUM? - ; - -rule11 - : - ; - -test3 - : LOREM IPSUM? rule13 AMET+ CONSECTETUR - ; - -rule13 - : (DOLOR | SIT)* - ; - -test4 - : LOREM (rule15 | rule16) - ; - -rule15 - : IPSUM DOLOR SIT - ; - -rule16 - : IPSUM DOLOR AMET - ; - -test5 - : LOREM (rule15 | rule16) - ; - -rule18 - : IPSUM DOLOR (SIT | CONSECTETUR) - ; - -rule19 - : IPSUM DOLOR AMET - ; - -test6 - : LOREM (rule15 | rule16) - ; - -rule21 - : IPSUM DOLOR SIT - ; - -rule22 - : IPSUM DOLOR (AMET | CONSECTETUR) - ; - -test7 - : LOREM (IPSUM DOLOR SIT | IPSUM DOLOR AMET) - ; - -test8 - : LOREM (IPSUM DOLOR SIT AMET | IPSUM DOLOR SIT CONSECTETUR) - ; - -LOREM - : 'LOREM' - ; - -IPSUM - : 'IPSUM' - ; - -DOLOR - : 'DOLOR' - ; - -SIT - : 'SIT' - ; - -AMET - : 'AMET' - ; - -CONSECTETUR - : 'CONSECTETUR' - ; - -ADIPISCING - : 'ADIPISCING' - ; - -WS - : [ \n\r\t] -> skip - ; diff --git a/tests/CPP14.g4 b/tests/CPP14.g4 index 3ca72e7..2360187 100644 --- a/tests/CPP14.g4 +++ b/tests/CPP14.g4 @@ -640,13 +640,8 @@ virtspecifierseq: virtspecifier | virtspecifierseq virtspecifier; virtspecifier: Override | Final; -/* purespecifier: - '=' '0'//Conflicts with the lexer - ; - */ -purespecifier: - Assign val = Octalliteral {if($val.text != "0") throw new antlr.InputMismatchException(this);} + Assign ZeroLiteral ; /*Derived classes*/ @@ -1093,15 +1088,18 @@ literal: ; Integerliteral: - Decimalliteral Integersuffix? + ZeroLiteral Integersuffix? + | Decimalliteral Integersuffix? | Octalliteral Integersuffix? | Hexadecimalliteral Integersuffix? | Binaryliteral Integersuffix? ; +ZeroLiteral: '0'; + Decimalliteral: NONZERODIGIT ( '\''? DIGIT)*; -Octalliteral: '0' ( '\''? OCTALDIGIT)*; +Octalliteral: '0' ( '\''? OCTALDIGIT)+; Hexadecimalliteral: ( '0x' | '0X') HEXADECIMALDIGIT ( '\''? HEXADECIMALDIGIT)*; @@ -1198,7 +1196,8 @@ userdefinedliteral: ; Userdefinedintegerliteral: - Decimalliteral Udsuffix + ZeroLiteral Udsuffix + | Decimalliteral Udsuffix | Octalliteral Udsuffix | Hexadecimalliteral Udsuffix | Binaryliteral Udsuffix