Nice TWiki > Doc > NiceGrammar (r1.3) TWiki webs:
Dev | Doc | Main | TWiki | Sandbox
Doc . { Changes | Index | Search | Go }
Here the EBNF of Nice. This version may differ on detail with the parser, but it should be almost the same.

The ( ... )? rules are replaced by [ ... ] for better readability.

last updated : 03 May 2003 (0.7.9 syntax without the deprecated things)

Declarations

module 
   : [ "package" name ";" ]  
      ( "import" name [ ".*" ] ";" )* 
      ( definition )*
       <EOF>
   ;

definition
   : globalVariable
   | classDefinition
   | interfaceDefinition
   | abstractInterfaceDefinition
   | abstractInterfaceImplementation
   | methodBodyDefinition
   | methodDeclaration
   ;

globalVariable
   : "var" monotype <IDENT> 
      [ "=" ( conditionalExpression | funExp ) ]
      ";"
   | "let" monotype <IDENT> "=" 
      ( conditionalExpression | funExp )
      ";"
   ;

interfaceDefinition
   : visibilityModifier "interface" <IDENT> 
      [ classTypeParameters ] 
      ["extends" extensionList ]
       classBody
   ;


classDefinition   
   : visibilityModifier 
      [ "final" | "abstract" ] 
      "class" <IDENT> 
      [ classTypeParameters ]
        [ "extends" extension ]
        [ "implements" extensionList ]
      [ "finally" "implements" extensionList ]
      classBody
   ;

abstractInterfaceDefinition
   : "abstract" "interface" <IDENT> 
      [ classTypeParameters ] 
      [ "extends" extensionList ]
      "{"
        ( visibilityModifier internalMethodOrFunction )*
        "}"
   ;

abstractInterfaceImplementation
   : "class" fullyQualifiedName 
      [ "finally" ] 
      "implements" <IDENT> ";"
   ;

extensionList
   : extension ( "," extension )*
   ;

extension
   : name typeParameters
   ;

visibilityModifier
   : "public"
   | "private"
   | 
   ;

classBody 
   : "{"   
      ( field
      | visibilityModifier
         ( methodBodyDefinition
         | internalMethodOrFunction
         )
      )*
        "}"
   | "=" "native" [ name ]  ";"
   ;


internalMethodOrFunction
   : constraint monotype 
      (<IDENT> | BACKQUOTEDSTRING ) 
      "(" formalParameters ")" contract 
      ( ";" | methodBody )
    ;

field
   : ( 
      "public-read" [ "private-write" ] 
     |  
      "private-write"  
     |    
      visibilityModifier [ "final" ]
     )      
        [ "transient" ]
        [ "volatile"  ]
          monoSymbol
        [ "=" expression ]
        ";" 
   ;


methodBodyDefinition
   : [ typeParameters ] 
      IdentExp
        "(" 
      [ pattern ( "," pattern )* ]  
      ")" methodBody
   ;

methodDeclaration
   : visibilityModifier constraint monotype IdentExp
        "(" formalParameters ")"
      ( "=" 
         (
             "native" nativeMethodDeclaration
         |
             "inline" name "(" [ <STRING_LITERAL> ] ")"
         )
         ";"
      | contract
         ( methodBody | ";" )
      )
   ;

nativeMethodDeclaration
   : ( "new" | doted_array_string ) 
        [ 
         name "(" 
         [ doted_array_string ( "," doted_array_string )* ]
          ")" 
        ]
      ";"
   ;

formalParameters
   : formalParameter ( "," formalParameter )*
   ;

formalparameter
   : monotype 
      [ <IDENT> 
         [ "=" expression ]
      ] 
   ;

pattern
   : (
       patternLiteral
     |
       ( "@" | "#" ) name
     |
       <IDENT>    
      [ "@" 
         ( 
            patternLiteral 
         | 
            name [ ":" monotype ]
         )
      | "#" name
      ]
     ) 
      [ "(" name ")" ] 
   ;

patternLiteral
   : "null" 
   | [ "-" ] <INT_LITERAL>
   | <CHAR_LITERAL>
   ;

contract
   : [ "requires" contractelements ]
       [ "ensures" contractelements ]
   ;

contractelements
   : contractelement ( "," [ contractelement ] )*
   ;

contractelement
   : conditionalExpression [ ":" conditionalExpression ] 
   ;

methodBody
   : block
   | "="  ( "..." | expression ";" )
   ;

Statements

block
   : "{" ( blockStatement )* "}"
   ;

blockStatement
   : localDeclaration
   | localFunctionDeclaration
   | localTupleDeclaration
   | statement
   ;

shortStatement
   : rootShortStatement
   | statementExpression 
   ;


rootShortStatement
   : block
   | shortIfStatement
   | "assert" contractelement
   | "throw" expression
   ;

statement
   : ";"
   | block
   | statementExpression ";"
   | ifStatement
   | forStatement
   | doStatement
   | whileStatement
   | tryStatement
   | "throw" expression ";"
   | "assert" contractelement ";"
   | "synchronized" "(" expression ")" block
   | "break" [ <IDENT> ] ";"
   | "continue" [ <IDENT> ] ";"
   | "return" [ expression ] ";"
   | <IDENT> ":" statement
   ;

statementExpression
   : ( "++" | "--" ) primaryExpression
   |  primaryExpression
      [ "++" | "--" 
      | assignmentOp expression
      ]
   ;

localDeclaration
   : ("let" | "var") 
      ( 
         monotype <IDENT> 
         [ "=" expression ] 
         ( "," <IDENT> 
            [ "=" expression ]
         )*
          ";"    
        | 
         <IDENT> "=" expression 
         ( "," <IDENT> "=" expression )*
          ";"    
      )
   | monotype <IDENT> 
      [ "=" expression ] 
      ( "," <IDENT> 
         [ "=" expression ]
      )*
      ";"    
   ;

localFunctionDeclaration
   : [ "let" | "var" ] 
      monotype <IDENT> "(" formalparameters ")" 
      methodBody 
   ;

localTupleDeclaration
   : "(" localTuplePart 
      ( "," localTuplePart )+
      ")" "=" expression ";" 
   ;

localTuplePart
   : "(" localTuplePart 
      ( "," localTuplePart )+
      ")"
   | [ monotype ] <IDENT> 
   ;

ifStatement
   : "if" "(" expression ")" statement 
      [ "else" statement ]
   ;

shortIfStatement
   : "if" "(" expression ")" shortStatement 
      [ "else" shortStatement ]
   ;

whileStatement
   : "while" "(" expression ")" statement
   ;

DoStatement
   : "do" statement "while" "(" expression ")" ";"
   ;

forStatement
   : "for" "(" 
      ( 
         monotype <IDENT> ":" expression
      | 
         ( forInit | ";" ) 
         [ expression ] ";" 
         [ statementExpression ( "," statementExpression )* ] 
      ) 
      ")" statement
   ;

forInit
   : localDeclaration ( "," localDeclaration )*
   | statementExpression ";"
   ;


tryStatement
   : "try" block 
      ( "catch" "(" name <IDENT> ")" block )*
        [ "finally" block ]
   ;

Expressions

expression
   : funExp
   | classicExpression 
   ;

funExp
   : ( constraint "(" monoSymbols ")" 
     | monoSymbol
     )
        "=>" ( rootShortStatement | expression ) 
   ;

classicExpression
   : conditionalExpression ( "="mentOp expression ]
   ;

assignmentOp
   : "*="
   | "/="
   | "%="
   | "+="
   | "-="
   | "<<="
   | ">>="
   | ">>>="
   | "&="
   | "^="
   | "|="
   | "="
   ;

conditionalExpression
   : conditionalOrExpression 
      [ "?" conditionalExpression ":" conditionalExpression ] 
   ;

conditionalOrExpression
   : conditionalAndExpression ( "||" conditionalAndExpression )*
   ;

conditionalAndExpression
   : inclusiveOrExpression ( "&&" inclusiveOrExpression )*
   ;

inclusiveOrExpression
   :  exclusiveOrExpression ( "|" exclusiveOrExpression )*
   ;

exclusiveOrExpression
   : andExpression ( "^" andExpression )*
   ;

andExpression
   : equalityExpression ( "&" equalityExpression )*
   ;

equalityExpression
   :  instanceOfExpression ( ( "==" | "!=" ) instanceOfExpression )*
   ;

instanceOfExpression
   : relationalExpression  [ "instanceof" doted_array_string ]
   ;

relationalExpression 
   : shiftExpression 
      (
         ( ( ">" | ">=" ) shiftExpression )*
      | 
         ( ("<" | "<=" ) shiftExpression )*
      )
   ;

shiftExpression
   : additiveExpression ( ( "<<" | ">>" | ">>>" ) additiveExpression )*
   ;

additiveExpression
   : multiplicativeExpression ( ( "-" | "+" ) multiplicativeExpression )*
   ;

multiplicativeExpression
   : unaryExpression ( ( "*" | "/" | "%" ) unaryExpression )*
   ;

unaryExpression
   : ( "-" | "+" ) primaryExpression 
   | ( "++" | "--" ) primaryExpression
   | ( "!" | "~" ) primaryExpression
        | primaryExpression [ "++" | "--" ]
   ;

primaryExpression
   : primaryPrefix ( primarySuffix )*
        |  "new" name
      ( newArraySuffix 
      | arguments ( primarySuffix )*
        )
   ;

newArraySuffix 
        : "[" expression "]" 
      ( "[" [ expression ] "]" )*
   ;

primaryPrefix
   : literal
   | "super" 
   | tupleExp
   | arrayLiteralExp 
   | doted_array_string "." "class"
   | IdentExp
   ;

literal
   : "null"
   | <INT_LITERAL>
   | <FLOAT_LITERAL>
   | <CHAR_LITERAL>
   | <STRING_LITERAL>
   ;

tupleExp
   : "("  expression 
      [ "," expression ( "," expression )* ]
      ")"
   ;

arrayLiteralExp
   : "[" 
      [ expression ( "," expression )* ]
       "]"
   ;

primarySuffix
   : "[" expression "]"
   | "." IdentExp
   | arguments
   ;

arguments
     : "(" 
      [ argument ( "," argument )* ]
      ")"
   ;

argument
   : [ <IDENT> ":" ] expression
   ;

Identifiers and Types

name 
   : <IDENT> ( "." <IDENT> )*
   ;

fullyQualifiedName
   : <IDENT> ( "." <IDENT>)+
   ;

doted_array_string
   : name ( "[]" )*
   ;

monoSymbol
   : monotype <IDENT>
   ;

monoSymbols
   : monoSymbol ( "," monoSymbol )*
   ;

binder  
   : [ "Any" | "!" | name ] name
   ;

atomicConstraint
   : monotype "<:" monotype
   | name ":" name
   ;

atomics
   : atomicConstraint ( "," atomicConstraint )*
   ;

constraint
   : [ "<" binder 
      ( "," binder )* 
      [ "|" atomics ] 
      ">"
     ]
   ;

monotypeConstructor
   : name "<" [ monotypes ] ">"
   ;

funOrTupleMonotype
     : "(" [ monotypes ] ")" 
      [ "?" ] "->" monotype
   ;

monotype
   : simpleMonotype 
      [ arraytypeEnd ] 
      [ [ "?" ] "->" monotype ]
   ;  

arraytypeEnd
   : ( "[]" | "[?]" )+
   ;

simpleMonotype
   :  [ "?" | "!" ]
      ( funOrTupleMonotype 
      | "alike" [ "<" monotypes ">" ]
      | monotypeConstructor
      | name
      )
   ;

monotypes
   :  monotype ( "," monotype )*
   ;

IdentExp
   : <IDENT>
   | <BACKQUOTED_STRING>
   ;

typeParameters
   : [ "<" <IDENT> 
      ( "," <IDENT> )* 
      ">"
     ]
   ;

classTypeParameters
   : "<" taggedMonotypeVar 
      ( "," taggedMonotypeVar )* 
      [ "|" atomics ]
       ">"
   ;

taggedMonotypeVar
   : [ monotype ]  
      [ "+"  | "-" ]  
      [ "!" ]
       <IDENT>
   ;

-- ArjanB - 03 May 2003

Topic NiceGrammar . { Edit | Attach | Ref-By | Printable | Diffs | r1.4 | > | r1.3 | > | r1.2 | More }
Revision r1.3 - 03 May 2003 - 21:50 GMT - ArjanB
Parents: WebHome
Copyright © 1999-2003 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback.