// This file contains the rules for the tokenization before any parsing. // The these rules follow the syntax required by the pest library. // Doc link to pest library: https://docs.rs/pest/latest/pest/ program = _{ ( ( BeforeEOI+ ~ EOI ) | EmptyProgram ) } BeforeEOI = _{ ( statement | EmptyLine | OnlyOneEmptyLine ) } statement = _{ FirstExpression ~ Expression* ~ noise* ~ EndOfStatement? } EndOfStatement = _{ Semicolon | new_line } EmptyLine = _{ noise* ~ EndOfStatement } // Script file is only an empty line without an ending new line. OnlyOneEmptyLine = _{ noise+ } // Script file is completely empty. EmptyProgram = _{ EOI } FirstExpression = _{ noise* ~ Value } Expression = _{ noise ~ Value } noise = _{ white_space | tab } new_line = { NEWLINE } Value = _{ ( bool | Float | int | Escaped | Namespace | MathExprBegin | CommandArrayExpandBegin | CommandStrExpandBegin | MathExprEnd | TypeIdent | WordStartWithKeyWord | LanguageSymbols | Word ) } Escaped = { EscapedStringSigil | EscapedArraySigil } TypeIdent = { Colon ~ ( TypePrimitiveIdent | TypeHashMap | TypeBeeTreeMap | TypeArray ) } TypeHashMap = { "hmap" ~ TypeArray } TypeBeeTreeMap = { "bmap" ~ TypeArray } TypeInt = { "int" } TypeFloat = { "float" } TypeBool = { "bool" } TypeStr = { "str" } TypePrimitiveIdent = _{ TypeInt | TypeFloat | TypeBool | TypeStr } TypeArray = { OpenSquareBracket ~ TypePrimitiveIdent ~ ClosedSquareBracket } WordStartWithKeyWord = { "_"? ~ LanguageKeyWord ~ Word } AllNoise = _{ noise | new_line } white_space = { " "+ } Word = { ( !( ":" | ".." | Semicolon | "$" | "@" | "{" | "}" | Escaped ) ~ ( Alpha | "_" ) )+ } LanguageSymbols = _{ Sigils | KeyWord | Operators | SpecialChars } SpecialChars = _{ ScopeDelimiters | Quotations | Comma } NonWord = { AllNoise | SpecialChars } Alpha = { !NonWord ~ ANY } tab = { "\t"+ } bool = { "false" | "true" } Float = { int ~ Point ~ ASCII_DIGIT+ } OnlyZero = { "0" } int = { "-"? ~ ( ( "0"? ~ ( ASCII_NONZERO_DIGIT ~ ASCII_DIGIT* ) ) | OnlyZero ) } KeyWord = _{ LanguageKeyWord } LanguageKeyWord = _{ Return | Let | Drop | Export | Fn | If | ElseIf | Else | In | For | While | Match | Case | End | Continue | Break } Return = { "return" } Let = { "let" } Drop = { "drop" } Fn = { "fn" } If = { "if" } ElseIf = { Else ~ noise* ~ If } Else = { "else" } In = { "in" } For = { "for" } While = { "while" } Match = { "match" } Case = { "case" } End = { "end" } Export = { "export" } Continue = { "continue" } Break = { "break" } Operators = _{ StartDocstring | Assignments | BinaryOperators } Quotations = _{ EscapedQuotation | EscapedSingleQuotation | Quotation | SingleQuotation } // Used to make `\"`, escpaded quotation, no op for string delimiting EscapedQuotation = { "\\" ~ "\"" } // Used to make `\'`, escpaded single quotation, no op for string delimiting EscapedSingleQuotation = { "\\" ~ "'" } Sigils = _{ StringSigil | ArraySigil } ScopeDelimiters = _{ OpenSquareBracket | ClosedSquareBracket | OpenCurlyBracket | ClosedCurlyBracket | OpenParanteseCurlyBracket | ClosedParanteseCurlyBracket } Assignments = _{ AddAssignment | MinusAssignment | PowerAssignment | MultAssignment | InterDivAssignment | DivAssignment | AppendAssignment | PrependAssignment | StripAssignment | Compare | Equal } StartDocstring = { "--" } Compare = {"=="} Equal = { "=" } AddAssignment = { "+=" } MinusAssignment = { "-=" } MultAssignment = { "*=" } InterDivAssignment = { "//=" } DivAssignment = { "/=" } PowerAssignment = { "**=" } StripAssignment = { "\\\\=" } AppendAssignment = { "++=" } PrependAssignment = { "::=" } BinaryOperators = _{ Add | Subtract | Power | Multiply | IntegerDivide | Divide | TriplePoint | DoublePoint | Hash | Colon | Point | ChainOperator } Comma = { "," } Point = { "." } Hash = { "#" } Colon = { ":" } Add = { "+" } Subtract = { "-" } Power = { "**" } Multiply = { "*" } IntegerDivide = { "//" } Divide = { "/" } DoublePoint = { ".." } TriplePoint = { "..." } ChainOperator = _{ ( Disown | PipeStdErr | PipeBoth | AndOperator | OrOperator | AppendToStdBoth | AppendToStdErr | AppendToStdOut | ToStdBoth | ToStdErr | ToStdOut | FileInputOpreator | Ampersand | Pipe ) } Ampersand = { "&" } Pipe = { "|" } AndOperator = { Ampersand ~ Ampersand } OrOperator = { Pipe ~ Pipe } AppendToStdOut = { ">>" } AppendToStdErr = { "^>>" } AppendToStdBoth = { "&>>" } ToStdOut = { ">" } ToStdErr = { "^>" } ToStdBoth = { "&>" } PipeStdErr = { "^|" } PipeBoth = { "&|" } Disown = { "&!" } Semicolon = { ";" } FileInputOpreator = { "<" } Quotation = { "\"" } SingleQuotation = { "'" } StringSigil = { "$" } ArraySigil = { "@" } EscapedStringSigil = { "\\" ~ StringSigil } EscapedArraySigil = { "\\" ~ ArraySigil } OpenSquareBracket = { "[" } ClosedSquareBracket = { "]" } OpenCurlyBracket = { "{" } ClosedCurlyBracket = { "}" } OpenParanteseCurlyBracket = { "(" } ClosedParanteseCurlyBracket = { ")" } Namespace = _{ NamespaceGlobal | NamespaceSuper | NamespaceColor | NamespaceEnv } NamespaceSign = { Colon ~ Colon } NamespaceGlobal = { "global" ~ NamespaceSign } NamespaceSuper = { "super" ~ NamespaceSign } NamespaceColor = { "c" ~ NamespaceSign } NamespaceEnv = { "env" ~ NamespaceSign } MathExprBegin = { StringSigil ~ OpenParanteseCurlyBracket ~ OpenParanteseCurlyBracket } CommandStrExpandBegin = { StringSigil ~ OpenParanteseCurlyBracket } CommandArrayExpandBegin = { ArraySigil ~ OpenParanteseCurlyBracket } MathExprEnd = { ClosedParanteseCurlyBracket ~ ClosedParanteseCurlyBracket }