#---------------------------------------------------------------------------* # # @file gtl_scanner.galgas # # @section desc File description # # scanner for GTL. # # @section copyright Copyright # # Goil OIL compiler, part of Trampoline RTOS # # Trampoline is copyright (c) CNRS, University of Nantes, # Ecole Centrale de Nantes # Trampoline is protected by the French intellectual property law. # # This software is distributed under the GNU Public Licence V2. # Check the LICENSE file in the root directory of Trampoline # # $Date$ # $Rev$ # $Author$ # $URL$ # #---------------------------------------------------------------------------* template lexique gtl_scanner { template "%" ... "%" replace "\\%" -> "%" replace "\\n" -> "\n" replace "\\\\" -> "\\" @string identifierString @bigint intValue @string tokenString @string functionContent style keywordsStyle -> "Keywords" style delimitersStyle -> "Delimiters" style nonTerminalStyle -> "Non Terminal Symbols" style integerStyle -> "Integer Constants" style floatCsts -> "Floating Point Constants" style stringStyle -> "String literals" style commentStyle -> "Comments" style characterStyle -> "Character Constants" # -------------------------- identifier or lkey word -------------------- # keywords can be written in lower or upper case # for identifiers, case is significant $identifier$ ! tokenString error message "an identifier" list goilTemplateKeyWordList style keywordsStyle error message "the '%K' keyword" { "after", "before", "between", "by", "default", "display", "do", "down", "else", "elsif", "emptylist", "emptymap", "end", "error", "exists", "false", "for", "foreach", "from", "func", "here", "if", "in", "import", "listof", "let", "loop", "mapof", "mod", "no", "not", "or", "print", "println", "repeat", "sort", "step", "tab", "template", "then", "to", "true", "typeof", "up", "yes", "warning", "while", "write", "executable", "variables", "getter", "unlet", "setter", "libraries", "input", "break", "__VARS__" } rule 'a'->'z' | 'A'->'Z' | '_' { repeat enterCharacterIntoString ( !?identifierString !*) enterCharacterIntoString ( !?tokenString !*) while 'a'->'z' | 'A'->'Z' | '_' | '0'->'9' : end send search identifierString in goilTemplateKeyWordList default $identifier$ } # ---------------------------------- enum value-------------------------------- $literal_enum$ ! tokenString error message "a literal enum value" rule '$' { repeat while 'a'->'z' | 'A'->'Z' | '_' | '0'->'9' : enterCharacterIntoString ( !?tokenString !*) end send $literal_enum$ } # ----------------------------- Delimitors --------------------------------------- list galgasDelimitorsList style delimitersStyle error message "the '%K' delimitor"{ "*", "|", ",", "+", "::", ">", ":", "(", ")", "->", "?", "==", "!", ":=", "[", "]", "+=", "-=", "/", "!=", ">=", "&", "<=", "{", "}", "<", "^", ">>", "~", "<-", "<<", "@", "*=", "/=", "&=", "|=", "<<=", ">>=", "mod=", "^=", "@[", "@(", "@{", "[!", "@!", "@?" } rule list galgasDelimitorsList #------------------------------- Integer, floating Point Constant ------------------ @double floatValue $literal_double$ !floatValue style floatCsts error message "a float number" $signed_literal_integer_bigint$ !intValue style integerStyle error message "a bigint decimal number" $-$ style delimitersStyle error message "the - delimitor" message floatNumberConversionError : "invalid float number" message internalError : "internal error" rule '-' { select case '0'->'9': enterCharacterIntoString(!?tokenString !'-') enterCharacterIntoString(!?tokenString !*) repeat while '0'->'9' : enterCharacterIntoString(!?tokenString !*) while '_' : end select case '.' : enterCharacterIntoString(!?tokenString !'.') repeat while '0'->'9' : enterCharacterIntoString(!?tokenString !*) while '_' : end convertStringToDouble(!tokenString !?floatValue error floatNumberConversionError) send $literal_double$ default convertDecimalStringIntoBigInt(!tokenString !?intValue error internalError) send $signed_literal_integer_bigint$ end default send $-$ end } rule "0x" | "0X" { repeat while '0'->'9' | 'a'->'f' | 'A'->'F': enterCharacterIntoString(!?tokenString !*) end convertHexStringIntoBigInt(!tokenString !?intValue error internalError) send $signed_literal_integer_bigint$ } rule '0'->'9' { enterCharacterIntoString(!?tokenString !*) repeat while '0'->'9' : enterCharacterIntoString(!?tokenString !*) while '_' : end select case '.' : enterCharacterIntoString(!?tokenString !'.') repeat while '0'->'9' : enterCharacterIntoString(!?tokenString !*) while '_' : end convertStringToDouble(!tokenString !?floatValue error floatNumberConversionError) send $literal_double$ default convertDecimalStringIntoBigInt(!tokenString !?intValue error internalError) send $signed_literal_integer_bigint$ end } #------------------------------- Floating point Number ------------------ $.$ style delimitersStyle error message "the '.' delimitor" $.=$ style delimitersStyle error message "the '.=' delimitor" $...$ style delimitersStyle error message "the '...' delimitor" #--- Floating Point constant beginning with a dot rule '.' { select case '0'->'9' : enterCharacterIntoString(!?tokenString !'0') enterCharacterIntoString(!?tokenString !'.') enterCharacterIntoString(!?tokenString !*) repeat while '0'->'9' : enterCharacterIntoString(!?tokenString !*) while '_' : end convertStringToDouble(!tokenString !?floatValue error floatNumberConversionError) send $literal_double$ default select case ".." : send $...$ case "=" : send $.=$ default send $.$ end end } # ------------------------------------ String ---------------------------- message unterminatedLitteralString : "Unterminated literal string" @string a_string @char charValue @uint uint32value message invalideUnicodeDefinition4 : "\\u should be followed by exactly four hexadecimal digits" message invalideUnicodeDefinition8 : "\\U should be followed by exactly eight hexadecimal digits" message unassignedUnicodeValue : "this value does not correspond to an assigned Unicode point" message incorrectHTMLescapeSequence : "Invalid HTML sequence, should be '&...;'" message unknownHTMLescapeSequence : "Invalid &...; HTML sequence" message incorrectCharConstant : "incorrect literal character" $literal_char$ ! charValue style characterStyle error message "a character constant" rule '\'' { select case '\\' : select case 'f' : enterCharacterIntoCharacter (!?charValue !'\f' ) case 'n' : enterCharacterIntoCharacter (!?charValue !'\n' ) case 'r' : enterCharacterIntoCharacter (!?charValue !'\r' ) case 't' : enterCharacterIntoCharacter (!?charValue !'\t' ) case 'v' : enterCharacterIntoCharacter (!?charValue !'\v' ) case '\\' : enterCharacterIntoCharacter (!?charValue !'\\' ) case '0' : enterCharacterIntoCharacter (!?charValue !'\0' ) case '\'' : enterCharacterIntoCharacter (!?charValue !'\'' ) case 'u' : select case '0' -> '9' | 'a' -> 'f' | 'A' -> 'F' : enterHexDigitIntoUInt ( !* !?uint32value error internalError, internalError ) select case '0' -> '9' | 'a' -> 'f' | 'A' -> 'F' : enterHexDigitIntoUInt ( !* !?uint32value error internalError, internalError ) select case '0' -> '9' | 'a' -> 'f' | 'A' -> 'F' : enterHexDigitIntoUInt ( !* !?uint32value error internalError, internalError ) select case '0' -> '9' | 'a' -> 'f' | 'A' -> 'F' : enterHexDigitIntoUInt ( !* !?uint32value error internalError, internalError ) convertUnsignedNumberToUnicodeChar (!?uint32value !?charValue error unassignedUnicodeValue ) default error invalideUnicodeDefinition4 end default error invalideUnicodeDefinition4 end default error invalideUnicodeDefinition4 end default error invalideUnicodeDefinition4 end case 'U' : select case '0' -> '9' | 'a' -> 'f' | 'A' -> 'F' : enterHexDigitIntoUInt ( !* !?uint32value error internalError, internalError ) select case '0' -> '9' | 'a' -> 'f' | 'A' -> 'F' : enterHexDigitIntoUInt ( !* !?uint32value error internalError, internalError ) select case '0' -> '9' | 'a' -> 'f' | 'A' -> 'F' : enterHexDigitIntoUInt ( !* !?uint32value error internalError, internalError ) select case '0' -> '9' | 'a' -> 'f' | 'A' -> 'F' : enterHexDigitIntoUInt ( !* !?uint32value error internalError, internalError ) select case '0' -> '9' | 'a' -> 'f' | 'A' -> 'F' : enterHexDigitIntoUInt ( !* !?uint32value error internalError, internalError ) select case '0' -> '9' | 'a' -> 'f' | 'A' -> 'F' : enterHexDigitIntoUInt ( !* !?uint32value error internalError, internalError ) select case '0' -> '9' | 'a' -> 'f' | 'A' -> 'F' : enterHexDigitIntoUInt ( !* !?uint32value error internalError, internalError ) select case '0' -> '9' | 'a' -> 'f' | 'A' -> 'F' : enterHexDigitIntoUInt ( !* !?uint32value error internalError, internalError ) convertUnsignedNumberToUnicodeChar (!?uint32value !?charValue error unassignedUnicodeValue ) default error invalideUnicodeDefinition8 end default error invalideUnicodeDefinition8 end default error invalideUnicodeDefinition8 end default error invalideUnicodeDefinition8 end default error invalideUnicodeDefinition8 end default error invalideUnicodeDefinition8 end default error invalideUnicodeDefinition8 end default error invalideUnicodeDefinition8 end case '&' : repeat while ~ ";" error incorrectHTMLescapeSequence : enterCharacterIntoString (!?tokenString !* ) end convertHTMLSequenceToUnicodeCharacter (!?tokenString !?charValue error unknownHTMLescapeSequence ) default error incorrectCharConstant end case ' ' -> '\uFFFD' : enterCharacterIntoCharacter (!?charValue !* ) default error incorrectCharConstant end select case '\'' : send $literal_char$ default error incorrectCharConstant end } $string$ !a_string style stringStyle error message "literal string" rule '"' { repeat while '\\' : select case 'f' : enterCharacterIntoString (!?a_string !'\f' ) case 'n' : enterCharacterIntoString (!?a_string !'\n' ) case 'r' : enterCharacterIntoString (!?a_string !'\r' ) case 't' : enterCharacterIntoString (!?a_string !'\t' ) case 'v' : enterCharacterIntoString (!?a_string !'\v' ) case '\\' : enterCharacterIntoString (!?a_string !'\\' ) case '"' : enterCharacterIntoString (!?a_string !'"' ) case '\'' : enterCharacterIntoString (!?a_string !'\'' ) case '?' : enterCharacterIntoString (!?a_string !'?' ) case '&' : repeat while ~ ";" error incorrectHTMLescapeSequence : enterCharacterIntoString (!?identifierString !* ) end convertHTMLSequenceToUnicodeCharacter (!?identifierString !?charValue error unknownHTMLescapeSequence ) enterCharacterIntoString (!?a_string !charValue ) case 'u' : select case '0' -> '9' | 'a' -> 'f' | 'A' -> 'F' : enterHexDigitIntoUInt ( !* !?uint32value error internalError, internalError ) select case '0' -> '9' | 'a' -> 'f' | 'A' -> 'F' : enterHexDigitIntoUInt ( !* !?uint32value error internalError, internalError ) select case '0' -> '9' | 'a' -> 'f' | 'A' -> 'F' : enterHexDigitIntoUInt ( !* !?uint32value error internalError, internalError ) select case '0' -> '9' | 'a' -> 'f' | 'A' -> 'F' : enterHexDigitIntoUInt ( !* !?uint32value error internalError, internalError ) convertUnsignedNumberToUnicodeChar (!?uint32value !?charValue error unassignedUnicodeValue ) enterCharacterIntoString (!?a_string !charValue ) default error invalideUnicodeDefinition4 end default error invalideUnicodeDefinition4 end default error invalideUnicodeDefinition4 end default error invalideUnicodeDefinition4 end case 'U' : select case '0' -> '9' | 'a' -> 'f' | 'A' -> 'F' : enterHexDigitIntoUInt ( !* !?uint32value error internalError, internalError ) select case '0' -> '9' | 'a' -> 'f' | 'A' -> 'F' : enterHexDigitIntoUInt ( !* !?uint32value error internalError, internalError ) select case '0' -> '9' | 'a' -> 'f' | 'A' -> 'F' : enterHexDigitIntoUInt ( !* !?uint32value error internalError, internalError ) select case '0' -> '9' | 'a' -> 'f' | 'A' -> 'F' : enterHexDigitIntoUInt ( !* !?uint32value error internalError, internalError ) select case '0' -> '9' | 'a' -> 'f' | 'A' -> 'F' : enterHexDigitIntoUInt ( !* !?uint32value error internalError, internalError ) select case '0' -> '9' | 'a' -> 'f' | 'A' -> 'F' : enterHexDigitIntoUInt ( !* !?uint32value error internalError, internalError ) select case '0' -> '9' | 'a' -> 'f' | 'A' -> 'F' : enterHexDigitIntoUInt ( !* !?uint32value error internalError, internalError ) select case '0' -> '9' | 'a' -> 'f' | 'A' -> 'F' : enterHexDigitIntoUInt ( !* !?uint32value error internalError, internalError ) convertUnsignedNumberToUnicodeChar (!?uint32value !?charValue error unassignedUnicodeValue ) enterCharacterIntoString (!?a_string !charValue ) default error invalideUnicodeDefinition8 end default error invalideUnicodeDefinition8 end default error invalideUnicodeDefinition8 end default error invalideUnicodeDefinition8 end default error invalideUnicodeDefinition8 end default error invalideUnicodeDefinition8 end default error invalideUnicodeDefinition8 end default error invalideUnicodeDefinition8 end default error incorrectCharConstant end while ' ' | '!' | '#'-> '\uFFFD' : enterCharacterIntoString (!?a_string !* ) end select case '"' : send $string$ default error unterminatedLitteralString end } # ------------------------------------ Comment ---------------------------- $comment$ style commentStyle error message "a comment" rule '#' { repeat while '\u0001' -> '\u0009' | '\u000B' | '\u000C' | '\u000E' -> '\uFFFD' : end drop $comment$ } # --------------------- separators ----------------------------------------- rule '\u0001' -> ' ' { } }