compiler/ast/parser

  Source   Edit

This module implements the parser of the standard Nimskull syntax. The parser strictly reflects the grammar ("doc/grammar.txt"); however it uses several helper routines to keep the parser small. A special efficient algorithm is used for the precedence levels. The parser here can be seen as a refinement of the grammar, as it specifies how the AST is built from the grammar and how comments belong to the AST.

Types

Parser = object
  currInd: int               ## current indentation level
  firstTok: bool             ## Has the first token been read?
  hasProgress: bool          ## some while loop requires progress ensurance
  lex*: Lexer                ## The lexer that is used for parsing
  tok*: Token                ## The current token
  lineStartPrevious*: int
  lineNumberPrevious*: int
  bufposPrevious*: int
  inPragma*: int             ## Pragma level
  inSemiStmtList*: int
  emptyNode: ParsedNode
  diags*: ParseDiags
A Parser object represents a file that is being parsed   Source   Edit
ParserError = object of CatchableError
  instLoc*: InstantiationInfo
  Source   Edit

Procs

proc closeParser(p: var Parser) {....raises: [], tags: [].}
Close a parser, freeing up its resources.   Source   Edit
proc openParser(p: var Parser; fileIdx: FileIndex; inputStream: PLLStream;
                cache: IdentCache; config: ConfigRef) {.
    ...raises: [IOError, Exception, ValueError, ERecoverableError],
    tags: [ReadIOEffect, RootEffect].}
Open a parser, using the given arguments to set up its internal state.   Source   Edit
proc openParser(p: var Parser; filename: AbsoluteFile; inputStream: PLLStream;
                cache: IdentCache; config: ConfigRef) {.
    ...raises: [IOError, Exception, ValueError, ERecoverableError, KeyError],
    tags: [ReadIOEffect, RootEffect, ReadDirEffect].}
  Source   Edit
proc parseAll(p: var Parser): ParsedNode {.
    ...raises: [IOError, Exception, ValueError, ERecoverableError],
    tags: [ReadIOEffect, RootEffect].}
Parses the rest of the input stream held by the parser into a ParsedNode.   Source   Edit
proc parseString(s: string; cache: IdentCache; config: ConfigRef;
                 filename: string = ""; line: int = 0): ParsedNode {.
    ...raises: [IOError, Exception, ValueError, ERecoverableError, KeyError],
    tags: [ReadIOEffect, RootEffect, ReadDirEffect].}
Parses a string into an AST, returning the top node. filename and line, although optional, provide info so that the compiler can generate correct error messages referring to the original source.   Source   Edit
proc parseTopLevelStmt(p: var Parser): ParsedNode {.
    ...raises: [Exception, ERecoverableError, IOError, ValueError],
    tags: [RootEffect, ReadIOEffect].}
Implements an iterator which, when called repeatedly, returns the next top-level statement or emptyNode if end of stream.   Source   Edit