-- @authors Frédéric Jouault -- @date 2007/09/19 -- @description This TCS model defines the syntax of the TCS language. -- REMARK: to use this tcs for extraction: remove the alternatives for keywords -- Identifiers are ALPHA (ALPHA | DIGIT)* with a special form between single quote to escape them in case they are the same as a keywords of TCS -- Strings are double quoted -- Comments begin with -- and end with the end of the line syntax TCS { primitiveTemplate identifier for String default using NAME: value = "%token%"; primitiveTemplate identifierOrKeyword for String using NAME orKeyword: value = "%token%"; primitiveTemplate stringSymbol for String using STRING: value = "%token%", serializer="'\\\"' + %value%.toCString() + '\\\"'"; primitiveTemplate integerSymbol for Integer default using INT: value = "Integer.valueOf(%token%)"; template ConcreteSyntax main context : "syntax" name (isDefined(k) ? "(" "k" "=" k ")") "{" [ templates (isDefined(keywords) ? "keywords" "{" [ keywords ] "}") (isDefined(symbols) ? "symbols" "{" [ symbols ] "}") operatorLists tokens (isDefined(lexer) ? "lexer" "=" lexer{as = stringSymbol} ";") ] {nbNL = 2} "}" ; -- @begin Templates template Template abstract; template PrimitiveTemplate : "primitiveTemplate" name "for" typeName (isDefault ? "default") "using" tokenName (orKeyword ? "orKeyword") ":" "value" "=" value{as = stringSymbol} (isDefined(serializer) ? "," "serializer" "=" serializer{as = stringSymbol} ) ";" ; template EnumerationTemplate : "enumerationTemplate" name (automatic ? "auto") (isDefined(mappings) ? [ ":" [ mappings{separator = ","} ] {startNL = false} ";" ] : ";" ) ; template EnumLiteralMapping : literal "=" element ; template ClassTemplate : "template" name (isDefined(mode) ? "#" mode) (isDefined(disambiguate) ? "(" "disambiguate" "=" disambiguate{as = stringSymbol} ")") (isMulti ? "multi") (isMain ? "main") (isAbstract ? (isDeep ? "deep") "abstract") (isOperatored ? "operatored" (isDefined(operatorList) ? "(" operatorList{refersTo = name} (isDefined(parentheses) ? "," parentheses ) ")" ) ) (isContext ? "context") (isAddToContext ? "addToContext") (isNonPrimary ? "nonPrimary") (isDefined(prefixSequence) ? "prefix" [ ":" [ prefixSequence ] {startNL = false} ]) (isDefined(templateSequence) ? [ ":" [ templateSequence ] {startNL = false} ";" ] : ";") ; template Parentheses : "parens" "(" (isOff ? "off" : left "," right ) ")" ; template OperatorTemplate : "operatorTemplate" name "(" "operators" "=" operators{refersTo = name} "," "source" "=" source (isDefined(storeOpTo) ? "," "storeOpTo" "=" storeOpTo) (isDefined(storeRightTo) ? "," "storeRightTo" "=" storeRightTo) (isDefined(disambiguate) ? "," "disambiguate" "=" disambiguate{as = stringSymbol}) (isDefined(disambiguateV3) ? "," "disambiguateV3" "=" disambiguateV3{as = stringSymbol}) ")" (isContext ? "context") (isDefined(otSequence) ? [ ":" [ otSequence ] {startNL = false} ";" ] : ";") ; template FunctionTemplate addToContext : "function" name "(" className ")" [ ":" [ functionSequence ] {startNL = false} ";" ] ; -- @end Templates template Sequence : elements ; -- @begin Sequence Elements template SequenceElement abstract; template LiteralRef : -- COMMENT FOR EXTRACTOR -- [[ referredLiteral{refersTo = value, lookIn = #all, as = stringSymbol, autoCreate = ifmissing, createAs = Keyword} -- | -- "@" referredLiteral{refersTo = name} -- ]] ; template Property : name{as = identifierOrKeyword} (isDefined(propertyArgs) ? "{" propertyArgs{separator = ","} "}") ; template CustomSeparator : "<" name ">" ; template Block : "[" blockSequence "]" (isDefined(blockArgs) ? "{" blockArgs{separator = ","} "}") ; template ConditionalElement : "(" condition "?" thenSequence (isDefined(elseSequence) ? ":" elseSequence) ")" ; template PropertyInit : "{{" name "<-" value "}}" ; template Alternative : "[[" sequences{separator = "|"} "]]" (isMulti ? "*") ; template FunctionCall : "$" calledFunction{refersTo = name} ; -- @end Sequence Elements -- @begin Property Arguments template PropertyArg abstract; template RefersToPArg : "refersTo" "=" propertyName{as = identifierOrKeyword} ; template LookInPArg : "lookIn" "=" (propertyName = "#all" ? "#" "all" : propertyName{as = identifierOrKeyword, separator = "."}) ; template CreateInPArg : "createIn" "=" propertyName{as = identifierOrKeyword, separator = "."} ; template AsPArg : "as" "=" value ; template RefersToKeyPArg : "refersToKey" "=" (value ? "true" : "false") ; template SeparatorPArg : "separator" "=" separatorSequence ; template AutoCreatePArg : "autoCreate" "=" value ; enumerationTemplate AutoCreateKind : #always = "always", #ifmissing = "ifmissing", #never = "never" ; template ImportContextPArg : "importContext" ; template ForcedLowerPArg : "forcedLower" "=" value ; template CreateAsPArg : "createAs" "=" name{as = identifierOrKeyword} ; template ModePArg : "mode" "=" mode{as = identifierOrKeyword} ; -- @end Property Arguments -- @begin Block Arguments template BlockArg abstract; template NbNLBArg : "nbNL" "=" value ; template IndentIncrBArg : "indentIncr" "=" value ; template StartNLBArg : "startNL" "=" (value ? "true" : "false") ; template StartNbNLBArg : "startNbNL" "=" value ; template EndNLBArg : "endNL" "=" (value ? "true" : "false") ; -- @end Block Arguments -- @begin Literals -- template Literal abstract; template Symbol addToContext : name "=" value{as = stringSymbol} (isDefined(spaces) ? ":" spaces{separator = ","}) ";" ; enumerationTemplate SpaceKind : #leftSpace = "leftSpace", #leftNone = "leftNone", #rightSpace = "rightSpace", #rightNone = "rightNone" ; template Keyword addToContext : name "=" value{as = stringSymbol} ";" ; -- @end Literals -- @begin Operators template OperatorList addToContext : "operators" (isDefined(name) ? name) "{" [ priorities ] "}" ; template Priority : "priority" value (associativity = #left ? -- nothing : "," associativity ) "{" [ operators ] "}" ; template Operator addToContext : (isPostfix ? "postfix") name "=" (isDefined(literal) ? [[ literal{refersTo = name} | literal{refersTo = value, lookIn = #all, as = stringSymbol, autoCreate = ifmissing, createAs = Keyword} ]]) "," arity ";" ; enumerationTemplate Associativity : #left = "left", #right = "right" ; -- @end Operators -- @begin Expressions template Expression abstract; template AndExp : expressions{separator = "and"} ; template AtomExp abstract; template EqualsExp : propertyName{as = identifierOrKeyword} "=" value ; template BooleanPropertyExp : propertyName{as = identifierOrKeyword} ; template IsDefinedExp : "isDefined" "(" propertyName{as = identifierOrKeyword} ")" ; template OneExp : "one" "(" propertyName{as = identifierOrKeyword} ")" ; --@end Expressions -- @begin Values template Value abstract; template StringVal : symbol{as = stringSymbol} ; template IntegerVal : symbol ; template NegativeIntegerVal : "-" symbol ; template EnumLiteralVal : "#" name{as = identifierOrKeyword} ; -- @end Values -- @begin Tokens template Token : "token" name ":" pattern ";" ; template OrPattern : simplePatterns{separator = "|"} ; -- @begin SimplePatterns template SimplePattern abstract; template RulePattern -- : name "(" arguments{separator = ","} ")" : rule ; -- @begin Rules template Rule abstract; template WordRule : "word" "(" "start" "=" start "," "part" "=" part (isDefined(end) ? "," "end" "=" end ) (isDefined(words) ? "," "words" "=" "(" words{separator = ","} ")" ) ")" ; template Word : name{as = stringSymbol} ":" token ; template EndOfLineRule : "endOfLine" "(" "start" "=" (dropStart ? "drop") start ")" ; template MultiLineRule : "multiLine" "(" "start" "=" (dropStart ? "drop") start "," "end" "=" (dropEnd ? "drop") end (isDefined(esc) ? "," "esc" "=" esc (isDefined(escMappings) ? "," "(" escMappings{separator = ","} ")" ) ) ")" ; template Mapping abstract; template SimpleMapping : key "->" value ; template WildcardMapping : "." "->" "." ; template OctalMapping : "octal" ; template HexadecimalMapping : "hex" ; -- @end Rules -- template RuleArgument -- : name "=" value -- ; template StringPattern : name{as = stringSymbol} ; template ClassPattern : "[" name "]" ; -- @end SimplePatterns -- @end Tokens symbols { lsquare = "["; rsquare = "]"; dlsquare = "[["; drsquare = "]]"; excl = "!"; coma = ","; lparen = "("; rparen = ")"; lcurly = "{"; rcurly = "}"; dlcurly = "{{"; drcurly = "}}"; semi = ";"; colon = ":"; pipe = "|"; sharp = "#"; qmark = "?"; dollar = "$"; arobas = "@"; -- operator symbols point = "."; rarrow = "->"; minus = "-"; star = "*"; slash = "/"; plus = "+"; eq = "="; gt = ">"; lt = "<"; ge = ">="; le = "<="; ne = "<>"; larrow = "<-"; } token COMMENT : endOfLine(start = "--"); lexer = " %options testLiterals = false; NL : ( '\\r' '\\n' | '\\n' '\\r' //Improbable | '\\r' | '\\n' ) {newline();} ; WS : ( ' ' | '\\t' ) ; %protected DIGIT : '0'..'9' ; %protected ALPHA : 'a'..'z' | 'A'..'Z' | '_' //For Unicode compatibility (from 0000 to 00ff) | '\\u00C0' .. '\\u00D6' | '\\u00D8' .. '\\u00F6' | '\\u00F8' .. '\\u00FF' ; %protected SNAME %v2 options { %v2 testLiterals = true; %v2 } : (ALPHA) (ALPHA | DIGIT)* ; NAME : ( %v3 SNAME %v2 s:SNAME {if(s.getType() != SNAME) $setType(s.getType());} | '\\''! ( ESC | '\\n' {newline();} | ~('\\\\'|'\\\''|'\\n') )* '\\''! %v3 {setText(ei.unescapeString(getText(), 1));} ) ; INT : (DIGIT)+ %v2 (('.' DIGIT)=> '.' (DIGIT)+ {$setType(FLOAT);})? ; %v3 FLOAT : DIGIT+ '.' DIGIT* ; %protected ESC : '\\\\'! ( 'n' %v2{%setText(\"\\n\");} | 'r' %v2{%setText(\"\\r\");} | 't' %v2{%setText(\"\\t\");} | 'b' %v2{%setText(\"\\b\");} | 'f' %v2{%setText(\"\\f\");} | '\"' %v2{%setText(\"\\\"\");} | '\\'' %v2{%setText(\"\\'\");} | '\\\\' %v2{%setText(\"\\\\\");} | ( ('0'..'3') ( %v2 options { %v2 warnWhenFollowAmbig = false; %v2 } : ('0'..'7') ( %v2 options { %v2 warnWhenFollowAmbig = false; %v2 } : '0'..'7' )? )? | ('4'..'7') ( %v2 options { %v2 warnWhenFollowAmbig = false; %v2 } : ('0'..'7') )? ) { %v2 String s = %getText; %v2 int i; %v2 int ret = 0; %v2 String ans; %v2 for (i=0; i