Class SemanticChecker

All Implemented Interfaces:
IVoidVisitor

public class SemanticChecker extends DepthFirstVoidVisitor
The SemanticChecker visitor checks and report informations, warnings or errors for the following conditions:
  • when a JavaCodeProduction is to be generated (warning if return type is non "void", information otherwise) (JTB will alter the code),
  • when a BNFProduction is not to be generated (information),
  • when a to be generated BNFProduction has a return value other than "void" (warning) (JTB will alter the code),
  • when a JavaCodeProduction or a BNFProduction is not to be generated locally but is also not to be generated globally (warning) (as unnecessary),
  • when a JavaCodeProduction or a BNFProduction has a name reserved for an automatically generated JTB class (e.g. INode, INodeList, ...) or (error) (the project will not compile),
  • when a "void" JavaCodeProduction or BNFProduction is used in an assignment (error) (the project will not compile),
  • when a "void" JavaCodeProduction or BNFProduction is used in a return statement with expression (error) (the project will not compile),
  • when a user declared variable is not initialized (warning) (javac may complain while compiling the generated parser),
  • when there are extraneous parentheses in a production (warning) (should be better to remove them),
  • when a no node creation indicator appears in an ExpansionChoices in a syntactic lookahead
  • when a return statement is transformed (information).

Note: the warning:
"No blocks of Java code must exist within ExpansionUnit" (since the JTB first authors believed they are generally unnecessary in JTB grammars)
has been replaced by the following new information:
"Return statement in a Java block in production '...' . It will be transformed in an assign statement to the corresponding new parser class variable."
These blocks are now allowed - as for example JavaCC 4.2 grammar has a lot of Java code blocks -
but as JTB generates return statements to create and return the nodes corresponding to the productions and changes the production return types, JTB now creates an additional parser class variable to store the user return information and changes the corresponding return statements.

Note: the warning:
"JavaCodeProduction blocks must be specially handled."
has been replaced by the following new information:
"Non "void" JavaCodeProduction. Result type '...' will be changed into '...', and a parser class variable 'jtbrt_...' of type '...'will be added to hold the return value."
JTB now creates an additional parser class variable to store the user return information and changes the corresponding return statements.

Note: the warning:
"Non initialized user variable '...'. May lead to compiler error(s). Check in generated parser."
has been added (it may lead to unnecessary warnings).

Note : could be done: check that the JTB generated return variables do not collide with user variables.

This visitor is supposed to be run once and not supposed to be run in parallel threads (on the same grammar).

TODO check coverage
  • Constructor Details

    • SemanticChecker

      public SemanticChecker(GlobalDataBuilder aGdbv)
      Constructor.
      Parameters:
      aGdbv - - the GlobalDataBuilder visitor
  • Method Details

    • visit

      public void visit(JavaCCInput n)
      Visits a JavaCCInput node, whose children are the following :

      f0 -> JavaCCOptions()
      f1 -> "PARSER_BEGIN"
      f2 -> "("
      f3 -> IdentifierAsString()
      f4 -> ")"
      f5 -> CompilationUnit()
      f6 -> "PARSER_END"
      f7 -> "("
      f8 -> IdentifierAsString()
      f9 -> ")"
      f10 -> ( Production() )+
      f11 -> invalid input: '<' EOF >
      s: 1465207473

      Specified by:
      visit in interface IVoidVisitor
      Overrides:
      visit in class DepthFirstVoidVisitor
      Parameters:
      n - - the node to visit
    • visit

      public void visit(OptionBinding n)
      Visits a OptionBinding node, whose children are the following :

      f0 -> ( %0 invalid input: '<' IDENTIFIER >
      .. .. | %1 "LOOKAHEAD"
      .. .. | %2 "IGNORE_CASE"
      .. .. | %3 "static" )
      f1 -> "="
      f2 -> ( %0 IntegerLiteral()
      .. .. | %1 BooleanLiteral()
      .. .. | %2 StringLiteral() )
      f3 -> ";"

      Specified by:
      visit in interface IVoidVisitor
      Overrides:
      visit in class DepthFirstVoidVisitor
      Parameters:
      n - - the node to visit
    • visit

      public void visit(Production n)
      Visits a Production node, whose child is the following :

      f0 -> . %0 JavaCodeProduction()
      .. .. | %1 RegularExprProduction()
      .. .. | %2 TokenManagerDecls()
      .. .. | %3 BNFProduction()
      s: -120615333

      Specified by:
      visit in interface IVoidVisitor
      Overrides:
      visit in class DepthFirstVoidVisitor
      Parameters:
      n - - the node to visit
    • visit

      public void visit(JavaCodeProduction n)
      Visits a JavaCodeProduction node, whose children are the following :

      f0 -> "JAVACODE"
      f1 -> AccessModifier()
      f2 -> ResultType()
      f3 -> IdentifierAsString()
      f4 -> FormalParameters()
      f5 -> [ #0 "throws" #1 Name()
      .. .. . #2 ( $0 "," $1 Name() )* ]
      f6 -> [ "%" ]
      f7 -> Block()
      s: -763138104

      Specified by:
      visit in interface IVoidVisitor
      Overrides:
      visit in class DepthFirstVoidVisitor
      Parameters:
      n - - the node to visit
    • visit

      public void visit(BNFProduction n)
      Visits a BNFProduction node, whose children are the following :

      f0 -> AccessModifier()
      f1 -> ResultType()
      f2 -> IdentifierAsString()
      f3 -> FormalParameters()
      f4 -> [ #0 "throws" #1 Name()
      .. .. . #2 ( $0 "," $1 Name() )* ]
      f5 -> [ "!" ]
      f6 -> ":"
      f7 -> Block()
      f8 -> "{"
      f9 -> ExpansionChoices()
      f10 -> "}"
      s: 1323482450

      Specified by:
      visit in interface IVoidVisitor
      Overrides:
      visit in class DepthFirstVoidVisitor
      Parameters:
      n - - the node to visit
    • visit

      public void visit(ExpansionChoices n)
      Visits a ExpansionChoices node, whose children are the following :

      f0 -> Expansion()
      f1 -> ( #0 "|" #1 Expansion() )*
      s: -1726831935

      Specified by:
      visit in interface IVoidVisitor
      Overrides:
      visit in class DepthFirstVoidVisitor
      Parameters:
      n - - the node to visit
    • visit

      public void visit(Expansion n)
      Visits a Expansion node, whose children are the following :

      f0 -> ( #0 "LOOKAHEAD" #1 "(" #2 LocalLookahead() #3 ")" )?
      f1 -> ( ExpansionUnit() )+
      s: -2134365682

      Specified by:
      visit in interface IVoidVisitor
      Overrides:
      visit in class DepthFirstVoidVisitor
      Parameters:
      n - - the node to visit
    • visit

      public void visit(LocalLookahead n)
      Visits a LocalLookahead node, whose children are the following :

      f0 -> [ IntegerLiteral() ]
      f1 -> [ "," ]
      f2 -> [ ExpansionChoices() ]
      f3 -> [ "," ]
      f4 -> [ #0 "{"
      .. .. . #1 [ Expression() ]
      .. .. . #2 "}" ]
      s: -1879920786

      Specified by:
      visit in interface IVoidVisitor
      Overrides:
      visit in class DepthFirstVoidVisitor
      Parameters:
      n - - the node to visit
    • visit

      public void visit(ExpansionUnit n)
      Visits a ExpansionUnit node, whose child is the following :

      f0 -> . %0 #0 "LOOKAHEAD" #1 "(" #2 LocalLookahead() #3 ")"
      .. .. | %1 Block()
      .. .. | %2 #0 "[" #1 ExpansionChoices() #2 "]"
      .. .. | %3 ExpansionUnitTCF()
      .. .. | %4 #0 [ $0 PrimaryExpression() $1 "=" ]
      .. .. . .. #1 ( invalid input: '&'0 $0 IdentifierAsString() $1 Arguments()
      .. .. . .. .. . .. $2 [ "!" ]
      .. .. . .. .. | invalid input: '&'1 $0 RegularExpression()
      .. .. . .. .. . .. $1 [ ?0 "." ?1 invalid input: '<' IDENTIFIER > ]
      .. .. . .. .. . .. $2 [ "!" ] )
      .. .. | %5 #0 "(" #1 ExpansionChoices() #2 ")"
      .. .. . .. #3 ( invalid input: '&'0 "+"
      .. .. . .. .. | invalid input: '&'1 "*"
      .. .. . .. .. | invalid input: '&'2 "?" )?
      s: 1116287061

      Specified by:
      visit in interface IVoidVisitor
      Overrides:
      visit in class DepthFirstVoidVisitor
      Parameters:
      n - - the node to visit
    • visit

      public void visit(ExpansionUnitTCF n)
      Visits a ExpansionUnitTCF node, whose children are the following :

      f0 -> "try"
      f1 -> "{"
      f2 -> ExpansionChoices()
      f3 -> "}"
      f4 -> ( #0 "catch" #1 "("
      .. .. . #2 ( Annotation() )*
      .. .. . #3 [ "final" ]
      .. .. . #4 Name() #5 invalid input: '<' IDENTIFIER > #6 ")" #7 Block() )*
      f5 -> [ #0 "finally" #1 Block() ]
      s: 1601707097

      Specified by:
      visit in interface IVoidVisitor
      Overrides:
      visit in class DepthFirstVoidVisitor
      Parameters:
      n - - the node to visit
    • visit

      public void visit(RegularExprProduction n)
      Visits a RegularExprProduction node, whose children are the following :

      f0 -> [ %0 #0 "invalid input: '<'" #1 "*" #2 ">"
      .. .. | %1 #0 "invalid input: '<'" #1 invalid input: '<' IDENTIFIER >
      .. .. . .. #2 ( $0 "," $1 invalid input: '<' IDENTIFIER > )*
      .. .. . .. #3 ">" ]
      f1 -> RegExprKind()
      f2 -> [ #0 "[" #1 "IGNORE_CASE" #2 "]" ]
      f3 -> ":"
      f4 -> "{"
      f5 -> RegExprSpec()
      f6 -> ( #0 "|" #1 RegExprSpec() )*
      f7 -> "}"
      s: 484788342

      Specified by:
      visit in interface IVoidVisitor
      Overrides:
      visit in class DepthFirstVoidVisitor
      Parameters:
      n - - the node to visit
    • visit

      public void visit(TokenManagerDecls n)
      Visits a TokenManagerDecls node, whose children are the following :

      f0 -> "TOKEN_MGR_DECLS"
      f1 -> ":"
      f2 -> ClassOrInterfaceBody()
      s: -1566997219

      Specified by:
      visit in interface IVoidVisitor
      Overrides:
      visit in class DepthFirstVoidVisitor
      Parameters:
      n - - the node to visit
    • visit

      public void visit(RegExprSpec n)
      Visits a RegExprSpec node, whose children are the following :

      f0 -> RegularExpression()
      f1 -> [ "!" ]
      f2 -> [ Block() ]
      f3 -> [ #0 ":" #1 invalid input: '<' IDENTIFIER > ]
      s: -1949948808

      Specified by:
      visit in interface IVoidVisitor
      Overrides:
      visit in class DepthFirstVoidVisitor
      Parameters:
      n - - the node to visit
    • visit

      public void visit(VariableDeclarator n)
      Visits a VariableDeclarator node, whose children are the following :

      f0 -> VariableDeclaratorId()
      f1 -> [ #0 "=" #1 VariableInitializer() ]
      s: -484955779

      Specified by:
      visit in interface IVoidVisitor
      Overrides:
      visit in class DepthFirstVoidVisitor
      Parameters:
      n - - the node to visit
    • visit

      public void visit(Statement n)
      Visits a Statement node, whose child is the following :

      f0 -> . %00 LabeledStatement()
      .. .. | %01 AssertStatement()
      .. .. | %02 Block()
      .. .. | %03 EmptyStatement()
      .. .. | %04 #0 StatementExpression() #1 ";"
      .. .. | %05 SwitchStatement()
      .. .. | %06 IfStatement()
      .. .. | %07 WhileStatement()
      .. .. | %08 DoStatement()
      .. .. | %09 ForStatement()
      .. .. | %10 BreakStatement()
      .. .. | %11 ContinueStatement()
      .. .. | %12 ReturnStatement()
      .. .. | %13 ThrowStatement()
      .. .. | %14 SynchronizedStatement()
      .. .. | %15 TryStatement()
      s: 1394695492

      Specified by:
      visit in interface IVoidVisitor
      Overrides:
      visit in class DepthFirstVoidVisitor
      Parameters:
      n - - the node to visit
    • visit

      public void visit(Block n)
      Visits a Block node, whose children are the following :

      f0 -> "{"
      f1 -> ( BlockStatement() )*
      f2 -> "}"
      s: -47169424

      Specified by:
      visit in interface IVoidVisitor
      Overrides:
      visit in class DepthFirstVoidVisitor
      Parameters:
      n - - the node to visit
    • visit

      public void visit(BlockStatement n)
      Visits a BlockStatement node, whose child is the following :

      f0 -> . %0 #0 LocalVariableDeclaration() #1 ";"
      .. .. | %1 Statement()
      .. .. | %2 ClassOrInterfaceDeclaration()
      s: -1009630136

      Specified by:
      visit in interface IVoidVisitor
      Overrides:
      visit in class DepthFirstVoidVisitor
      Parameters:
      n - - the node to visit
    • visit

      public void visit(LocalVariableDeclaration n)
      Visits a LocalVariableDeclaration node, whose children are the following :

      f0 -> VariableModifiers()
      f1 -> Type()
      f2 -> VariableDeclarator()
      f3 -> ( #0 "," #1 VariableDeclarator() )*
      s: 225808290

      Specified by:
      visit in interface IVoidVisitor
      Overrides:
      visit in class DepthFirstVoidVisitor
      Parameters:
      n - - the node to visit