Skip to content

Commit

Permalink
Updates for Go target.
Browse files Browse the repository at this point in the history
  • Loading branch information
kaby76 committed Jul 17, 2023
1 parent 6a078ec commit b66a9f4
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 12 deletions.
20 changes: 10 additions & 10 deletions cpp/CPP14Parser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -123,30 +123,30 @@ unaryExpression:
)
| Alignof LeftParen theTypeId RightParen
| noExceptExpression
| newExpression
| newExpression_
| deleteExpression;

unaryOperator: Or | Star | And | Plus | Tilde | Minus | Not;

newExpression:
newExpression_:
Doublecolon? New newPlacement? (
newTypeId
| (LeftParen theTypeId RightParen)
) newInitializer?;
) newInitializer_?;

newPlacement: LeftParen expressionList RightParen;

newTypeId: typeSpecifierSeq newDeclarator?;
newTypeId: typeSpecifierSeq newDeclarator_?;

newDeclarator:
pointerOperator newDeclarator?
newDeclarator_:
pointerOperator newDeclarator_?
| noPointerNewDeclarator;

noPointerNewDeclarator:
LeftBracket expression RightBracket attributeSpecifierSeq?
| noPointerNewDeclarator LeftBracket constantExpression RightBracket attributeSpecifierSeq?;

newInitializer:
newInitializer_:
LeftParen expressionList? RightParen
| bracedInitList;

Expand Down Expand Up @@ -300,7 +300,7 @@ declaration:
| explicitSpecialization
| linkageSpecification
| namespaceDefinition
| emptyDeclaration
| emptyDeclaration_
| attributeDeclaration;

blockDeclaration:
Expand All @@ -323,7 +323,7 @@ simpleDeclaration:
staticAssertDeclaration:
Static_assert LeftParen constantExpression Comma StringLiteral RightParen Semi;

emptyDeclaration: Semi;
emptyDeclaration_: Semi;

attributeDeclaration: attributeSpecifierSeq Semi;

Expand Down Expand Up @@ -628,7 +628,7 @@ memberdeclaration:
| staticAssertDeclaration
| templateDeclaration
| aliasDeclaration
| emptyDeclaration;
| emptyDeclaration_;

memberDeclaratorList:
memberDeclarator (Comma memberDeclarator)*;
Expand Down
37 changes: 37 additions & 0 deletions cpp/Go/cpp14_parser_base.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package parser

import (
"github.com/antlr4-go/antlr/v4"
// "reflect"
// "fmt"
)

// ParserBase implementation.
type CPP14ParserBase struct {
*antlr.BaseParser
}


// Returns true if the current Token is a closing bracket (")" or "}")
func (p *CPP14ParserBase) IsPureSpecifierAllowed() bool {
// fmt.Println("YO!")
x := p.GetParserRuleContext();
// fmt.Println("YO 1", x)
y := x.GetChild(0)
// fmt.Println("YO 2", y)
c := y.GetChild(0)
// fmt.Println("YO 3 ", c)
c2 := c.GetChild(0)
// fmt.Println("YO 4 ", c2)
if c2.GetChildCount() <= 1 {
// fmt.Println("return false")
return false
}
// c3 := c2.GetChild(1)
// if c3 == nil {
// return false
// }
// return reflect.TypeOf(c3).Elem() == reflect.TypeOf(ParametersAndQualifiersContext{}).Elem()
// fmt.Println("return true")
return true
}
31 changes: 31 additions & 0 deletions cpp/Go/transformGrammar.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import sys, os, re, shutil
from glob import glob
from pathlib import Path

def main(argv):
for file in glob("./parser/*.g4"):
fix(file)

def fix(file_path):
print("Altering " + file_path)
if not os.path.exists(file_path):
print(f"Could not find file: {file_path}")
sys.exit(1)
parts = os.path.split(file_path)
file_name = parts[-1]

shutil.move(file_path, file_path + ".bak")
input_file = open(file_path + ".bak",'r')
output_file = open(file_path, 'w')
for x in input_file:
if 'this.' in x:
x = x.replace('this.', 'p.')
output_file.write(x)
output_file.flush()

print("Writing ...")
input_file.close()
output_file.close()

if __name__ == '__main__':
main(sys.argv)
2 changes: 1 addition & 1 deletion cpp/desc.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="UTF-8" ?>
<desc xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../_scripts/desc.xsd">
<targets>Cpp;CSharp;Dart;Java;JavaScript;Python3;TypeScript</targets>
<targets>Cpp;CSharp;Dart;Go;Java;JavaScript;Python3;TypeScript</targets>
</desc>
2 changes: 1 addition & 1 deletion cpp/examples/pointer_declaration_example.cpp.tree
Original file line number Diff line number Diff line change
@@ -1 +1 @@
(translationUnit (declarationseq (declaration (functionDefinition (declSpecifierSeq (declSpecifier (typeSpecifier (trailingTypeSpecifier (simpleTypeSpecifier int))))) (declarator (pointerDeclarator (noPointerDeclarator (noPointerDeclarator (declaratorid (idExpression (unqualifiedId f)))) (parametersAndQualifiers ( ))))) (functionBody (compoundStatement { (statementSeq (statement (declarationStatement (blockDeclaration (simpleDeclaration (declSpecifierSeq (declSpecifier (typeSpecifier (trailingTypeSpecifier (simpleTypeSpecifier (theTypeName (className MyClass))))))) (initDeclaratorList (initDeclarator (declarator (pointerDeclarator (pointerOperator *) (noPointerDeclarator (declaratorid (idExpression (unqualifiedId test)))))) (initializer (braceOrEqualInitializer = (initializerClause (assignmentExpression (conditionalExpression (logicalOrExpression (logicalAndExpression (inclusiveOrExpression (exclusiveOrExpression (andExpression (equalityExpression (relationalExpression (shiftExpression (additiveExpression (multiplicativeExpression (pointerMemberExpression (castExpression (unaryExpression (newExpression new (newTypeId (typeSpecifierSeq (typeSpecifier (trailingTypeSpecifier (simpleTypeSpecifier (theTypeName (className MyClass)))))))))))))))))))))))))))) ;)))) (statement (expressionStatement (expression (assignmentExpression (conditionalExpression (logicalOrExpression (logicalAndExpression (inclusiveOrExpression (exclusiveOrExpression (andExpression (equalityExpression (relationalExpression (shiftExpression (additiveExpression (multiplicativeExpression (pointerMemberExpression (castExpression (unaryExpression (postfixExpression (postfixExpression (postfixExpression (primaryExpression (idExpression (unqualifiedId test)))) -> (idExpression (unqualifiedId foo))) ( )))))))))))))))))) ;))) }))))) <EOF>)
(translationUnit (declarationseq (declaration (functionDefinition (declSpecifierSeq (declSpecifier (typeSpecifier (trailingTypeSpecifier (simpleTypeSpecifier int))))) (declarator (pointerDeclarator (noPointerDeclarator (noPointerDeclarator (declaratorid (idExpression (unqualifiedId f)))) (parametersAndQualifiers ( ))))) (functionBody (compoundStatement { (statementSeq (statement (declarationStatement (blockDeclaration (simpleDeclaration (declSpecifierSeq (declSpecifier (typeSpecifier (trailingTypeSpecifier (simpleTypeSpecifier (theTypeName (className MyClass))))))) (initDeclaratorList (initDeclarator (declarator (pointerDeclarator (pointerOperator *) (noPointerDeclarator (declaratorid (idExpression (unqualifiedId test)))))) (initializer (braceOrEqualInitializer = (initializerClause (assignmentExpression (conditionalExpression (logicalOrExpression (logicalAndExpression (inclusiveOrExpression (exclusiveOrExpression (andExpression (equalityExpression (relationalExpression (shiftExpression (additiveExpression (multiplicativeExpression (pointerMemberExpression (castExpression (unaryExpression (newExpression_ new (newTypeId (typeSpecifierSeq (typeSpecifier (trailingTypeSpecifier (simpleTypeSpecifier (theTypeName (className MyClass)))))))))))))))))))))))))))) ;)))) (statement (expressionStatement (expression (assignmentExpression (conditionalExpression (logicalOrExpression (logicalAndExpression (inclusiveOrExpression (exclusiveOrExpression (andExpression (equalityExpression (relationalExpression (shiftExpression (additiveExpression (multiplicativeExpression (pointerMemberExpression (castExpression (unaryExpression (postfixExpression (postfixExpression (postfixExpression (primaryExpression (idExpression (unqualifiedId test)))) -> (idExpression (unqualifiedId foo))) ( )))))))))))))))))) ;))) }))))) <EOF>)

0 comments on commit b66a9f4

Please sign in to comment.