# Copyright 2022 Mattia Giambirtone & All Contributors # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import strformat type TokenType* {.pure.} = enum ## Token types enumeration # Booleans True, False, # Other singleton types Infinity, NotANumber, Nil # Control flow statements If, Else, # Looping statements While, For, # Keywords Function, Break, Continue, Var, Let, Const, Is, Return, Coroutine, Generator, Import, IsNot, Raise, Assert, Await, Foreach, Yield, Of, Defer, Try, Except, Finally, Type, Operator, Case, Enum, From, Emit, As, Ptr, Ref # Literal types Integer, Float, String, Identifier, Binary, Octal, Hex, Char # Brackets, parentheses, # operators and others LeftParen, RightParen, # () LeftBrace, RightBrace, # {} LeftBracket, RightBracket, # [] Dot, Semicolon, Colon, Comma, # . ; : , Plus, Minus, Slash, Star, # + - / * Percentage, DoubleStar, # % ** Caret, Pipe, Ampersand, Tilde, # ^ | & ~ Equal, GreaterThan, LessThan, # = > < LessOrEqual, GreaterOrEqual, # >= <= NotEqual, RightShift, LeftShift, # != >> << LogicalAnd, LogicalOr, LogicalNot, # and or not InplaceAdd, InplaceSub, InplaceDiv, # += -= /= InplaceMod, InplaceMul, InplaceXor, # %= *= ^= InplaceAnd, InplaceOr, FloorDiv, # &= |= // DoubleEqual, InplaceFloorDiv, InplacePow, # == //= **= InplaceRightShift, InplaceLeftShift, # >>= <<= # Miscellaneous EndOfFile, # Marks the end of the token stream NoMatch, # Used internally by the symbol table Comment, # Useful for documentation comments, pragmas, etc. # These are not used at the moment but may be # employed to enforce indentation or other neat # stuff I haven't thought about yet Whitespace, Tab, Token* = ref object ## A token object kind*: TokenType # Type of the token lexeme*: string # The lexeme associated to the token line*: int # The line where the token appears pos*: tuple[start, stop: int] # The absolute position in the source file # (0-indexed and inclusive at the beginning) proc `$`*(self: Token): string = if self != nil: result = &"Token(kind={self.kind}, lexeme='{$(self.lexeme)}', line={self.line}, pos=({self.pos.start}, {self.pos.stop}))" else: result = "nil"