# 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, Return, Coroutine, Generator, Import, Raise, Assert, Await, Foreach, Yield, Defer, Try, Except, Finally, Type, Operator, Case, Enum, From, 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, Comma, # . ; , # Miscellaneous EndOfFile, # Marks the end of the token stream NoMatch, # Used internally by the symbol table Comment, # Useful for documentation comments, pragmas, etc. Symbol, # A generic symbol # 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 = ## Strinfifies if self != nil: result = &"Token(kind={self.kind}, lexeme='{$(self.lexeme)}', line={self.line}, pos=({self.pos.start}, {self.pos.stop}))" else: result = "nil" proc `==`*(self, other: Token): bool = ## Returns self == other return self.kind == other.kind and self.lexeme == other.lexeme