summary refs log tree commit diff stats
path: root/compiler/pas2nim
diff options
context:
space:
mode:
authorAraq <rumpf_a@web.de>2014-07-15 09:37:45 +0200
committerAraq <rumpf_a@web.de>2014-07-15 09:37:45 +0200
commit6d3b632b47d626f6915dc2cbef8745baf04b8d76 (patch)
treecd2097090977d241065d1415bc9d0845689063f8 /compiler/pas2nim
parent41bb0bf9dcccdfcebdb0f823fea8b2853b89ea4e (diff)
parent5f8ab1653ad38faf395daf1b8a70249f6954b1ba (diff)
downloadNim-6d3b632b47d626f6915dc2cbef8745baf04b8d76.tar.gz
Merge branch 'new_spawn' into devel
Conflicts:
	todo.txt
	web/news.txt
	web/nimrod.ini
Diffstat (limited to 'compiler/pas2nim')
-rw-r--r--compiler/pas2nim/nimrod.cfg4
-rw-r--r--compiler/pas2nim/pas2nim.nim64
-rw-r--r--compiler/pas2nim/paslex.nim570
-rw-r--r--compiler/pas2nim/pasparse.nim1513
4 files changed, 0 insertions, 2151 deletions
diff --git a/compiler/pas2nim/nimrod.cfg b/compiler/pas2nim/nimrod.cfg
deleted file mode 100644
index cfeda63ed..000000000
--- a/compiler/pas2nim/nimrod.cfg
+++ /dev/null
@@ -1,4 +0,0 @@
-# Use the modules of the compiler
-
-path: "$nimrod/compiler"
-
diff --git a/compiler/pas2nim/pas2nim.nim b/compiler/pas2nim/pas2nim.nim
deleted file mode 100644
index d10028167..000000000
--- a/compiler/pas2nim/pas2nim.nim
+++ /dev/null
@@ -1,64 +0,0 @@
-#
-#
-#      Pas2nim - Pascal to Nimrod source converter
-#        (c) Copyright 2012 Andreas Rumpf
-#
-#    See the file "copying.txt", included in this
-#    distribution, for details about the copyright.
-#
-
-import
-  strutils, os, parseopt, llstream, ast, renderer, options, msgs,
-  paslex, pasparse
-
-const
-  Version = "0.8"
-  Usage = """
-pas2nim - Pascal to Nimrod source converter
-  (c) 2012 Andreas Rumpf
-Usage: pas2nim [options] inputfile [options]
-Options:
-  -o, --out:FILE         set output filename
-  --ref                  convert ^typ to ref typ (default: ptr typ)
-  --boot                 use special translation rules for the Nimrod compiler
-  -v, --version          write pas2nim's version
-  -h, --help             show this help
-"""
-
-proc main(infile, outfile: string, flags: set[TParserFlag]) =
-  var stream = llStreamOpen(infile, fmRead)
-  if stream == nil: rawMessage(errCannotOpenFile, infile)
-  var p: TParser
-  openParser(p, infile, stream, flags)
-  var module = parseUnit(p)
-  closeParser(p)
-  renderModule(module, outfile)
-
-var
-  infile = ""
-  outfile = ""
-  flags: set[TParserFlag] = {}
-for kind, key, val in getopt():
-  case kind
-  of cmdArgument: infile = key
-  of cmdLongOption, cmdShortOption:
-    case key
-    of "help", "h":
-      stdout.write(Usage)
-      quit(0)
-    of "version", "v":
-      stdout.write(Version & "\n")
-      quit(0)
-    of "o", "out": outfile = val
-    of "ref": incl(flags, pfRefs)
-    of "boot": flags = flags + {pfRefs, pfMoreReplacements, pfImportBlackList}
-    else: stdout.writeln("[Error] unknown option: " & key)
-  of cmdEnd: assert(false)
-if infile.len == 0:
-  # no filename has been given, so we show the help:
-  stdout.write(Usage)
-else:
-  if outfile.len == 0:
-    outfile = changeFileExt(infile, "nim")
-  infile = addFileExt(infile, "pas")
-  main(infile, outfile, flags)
diff --git a/compiler/pas2nim/paslex.nim b/compiler/pas2nim/paslex.nim
deleted file mode 100644
index f24b0c420..000000000
--- a/compiler/pas2nim/paslex.nim
+++ /dev/null
@@ -1,570 +0,0 @@
-#
-#
-#      Pas2nim - Pascal to Nimrod source converter
-#        (c) Copyright 2012 Andreas Rumpf
-#
-#    See the file "copying.txt", included in this
-#    distribution, for details about the copyright.
-#
-
-# This module implements a FreePascal scanner. This is an adaption from
-# the scanner module.
-
-import
-  hashes, options, msgs, strutils, platform, idents, nimlexbase, llstream
-
-const
-  MaxLineLength* = 80         # lines longer than this lead to a warning
-  numChars*: TCharSet = {'0'..'9', 'a'..'z', 'A'..'Z'}
-  SymChars*: TCharSet = {'a'..'z', 'A'..'Z', '0'..'9', '\x80'..'\xFF'}
-  SymStartChars*: TCharSet = {'a'..'z', 'A'..'Z', '\x80'..'\xFF'}
-  OpChars*: TCharSet = {'+', '-', '*', '/', '<', '>', '!', '?', '^', '.', '|',
-    '=', ':', '%', '&', '$', '@', '~', '\x80'..'\xFF'}
-
-# keywords are sorted!
-
-type
-  TTokKind* = enum
-    pxInvalid, pxEof,
-    pxAnd, pxArray, pxAs, pxAsm, pxBegin, pxCase, pxClass, pxConst,
-    pxConstructor, pxDestructor, pxDiv, pxDo, pxDownto, pxElse, pxEnd, pxExcept,
-    pxExports, pxFinalization, pxFinally, pxFor, pxFunction, pxGoto, pxIf,
-    pxImplementation, pxIn, pxInherited, pxInitialization, pxInline,
-    pxInterface, pxIs, pxLabel, pxLibrary, pxMod, pxNil, pxNot, pxObject, pxOf,
-    pxOr, pxOut, pxPacked, pxProcedure, pxProgram, pxProperty, pxRaise,
-    pxRecord, pxRepeat, pxResourcestring, pxSet, pxShl, pxShr, pxThen,
-    pxThreadvar, pxTo, pxTry, pxType, pxUnit, pxUntil, pxUses, pxVar, pxWhile,
-    pxWith, pxXor,
-    pxComment,                # ordinary comment
-    pxCommand,                # {@}
-    pxAmp,                    # {&}
-    pxPer,                    # {%}
-    pxStrLit, pxSymbol,       # a symbol
-    pxIntLit, pxInt64Lit, # long constant like 0x70fffffff or out of int range
-    pxFloatLit, pxParLe, pxParRi, pxBracketLe, pxBracketRi, pxComma,
-    pxSemiColon, pxColon,     # operators
-    pxAsgn, pxEquals, pxDot, pxDotDot, pxHat, pxPlus, pxMinus, pxStar, pxSlash,
-    pxLe, pxLt, pxGe, pxGt, pxNeq, pxAt, pxStarDirLe, pxStarDirRi, pxCurlyDirLe,
-    pxCurlyDirRi
-  TTokKinds* = set[TTokKind]
-
-const
-  Keywords = ["and", "array", "as", "asm", "begin", "case", "class", "const",
-    "constructor", "destructor", "div", "do", "downto", "else", "end", "except",
-    "exports", "finalization", "finally", "for", "function", "goto", "if",
-    "implementation", "in", "inherited", "initialization", "inline",
-    "interface", "is", "label", "library", "mod", "nil", "not", "object", "of",
-    "or", "out", "packed", "procedure", "program", "property", "raise",
-    "record", "repeat", "resourcestring", "set", "shl", "shr", "then",
-    "threadvar", "to", "try", "type", "unit", "until", "uses", "var", "while",
-    "with", "xor"]
-
-  firstKeyword = pxAnd
-  lastKeyword = pxXor
-
-type
-  TNumericalBase* = enum base10, base2, base8, base16
-  TToken* = object
-    xkind*: TTokKind          # the type of the token
-    ident*: PIdent            # the parsed identifier
-    iNumber*: BiggestInt      # the parsed integer literal
-    fNumber*: BiggestFloat    # the parsed floating point literal
-    base*: TNumericalBase     # the numerical base; only valid for int
-                              # or float literals
-    literal*: string          # the parsed (string) literal
-
-  TLexer* = object of TBaseLexer
-    filename*: string
-
-
-proc getTok*(L: var TLexer, tok: var TToken)
-proc printTok*(tok: TToken)
-proc `$`*(tok: TToken): string
-# implementation
-
-var
-  dummyIdent: PIdent
-  gLinesCompiled: int
-
-proc fillToken(L: var TToken) =
-  L.xkind = pxInvalid
-  L.iNumber = 0
-  L.literal = ""
-  L.fNumber = 0.0
-  L.base = base10
-  L.ident = dummyIdent        # this prevents many bugs!
-
-proc openLexer*(lex: var TLexer, filename: string, inputstream: PLLStream) =
-  openBaseLexer(lex, inputstream)
-  lex.filename = filename
-
-proc closeLexer*(lex: var TLexer) =
-  inc(gLinesCompiled, lex.LineNumber)
-  closeBaseLexer(lex)
-
-proc getColumn(L: TLexer): int =
-  result = getColNumber(L, L.bufPos)
-
-proc getLineInfo*(L: TLexer): TLineInfo =
-  result = newLineInfo(L.filename, L.linenumber, getColNumber(L, L.bufpos))
-
-proc lexMessage*(L: TLexer, msg: TMsgKind, arg = "") =
-  msgs.globalError(getLineInfo(L), msg, arg)
-
-proc lexMessagePos(L: var TLexer, msg: TMsgKind, pos: int, arg = "") =
-  var info = newLineInfo(L.filename, L.linenumber, pos - L.lineStart)
-  msgs.globalError(info, msg, arg)
-
-proc tokKindToStr*(k: TTokKind): string =
-  case k
-  of pxEof: result = "[EOF]"
-  of firstKeyword..lastKeyword:
-    result = Keywords[ord(k)-ord(firstKeyword)]
-  of pxInvalid, pxComment, pxStrLit: result = "string literal"
-  of pxCommand: result = "{@"
-  of pxAmp: result = "{&"
-  of pxPer: result = "{%"
-  of pxSymbol: result = "identifier"
-  of pxIntLit, pxInt64Lit: result = "integer literal"
-  of pxFloatLit: result = "floating point literal"
-  of pxParLe: result = "("
-  of pxParRi: result = ")"
-  of pxBracketLe: result = "["
-  of pxBracketRi: result = "]"
-  of pxComma: result = ","
-  of pxSemiColon: result = ";"
-  of pxColon: result = ":"
-  of pxAsgn: result = ":="
-  of pxEquals: result = "="
-  of pxDot: result = "."
-  of pxDotDot: result = ".."
-  of pxHat: result = "^"
-  of pxPlus: result = "+"
-  of pxMinus: result = "-"
-  of pxStar: result = "*"
-  of pxSlash: result = "/"
-  of pxLe: result = "<="
-  of pxLt: result = "<"
-  of pxGe: result = ">="
-  of pxGt: result = ">"
-  of pxNeq: result = "<>"
-  of pxAt: result = "@"
-  of pxStarDirLe: result = "(*$"
-  of pxStarDirRi: result = "*)"
-  of pxCurlyDirLe: result = "{$"
-  of pxCurlyDirRi: result = "}"
-
-proc `$`(tok: TToken): string =
-  case tok.xkind
-  of pxInvalid, pxComment, pxStrLit: result = tok.literal
-  of pxSymbol: result = tok.ident.s
-  of pxIntLit, pxInt64Lit: result = $tok.iNumber
-  of pxFloatLit: result = $tok.fNumber
-  else: result = tokKindToStr(tok.xkind)
-
-proc printTok(tok: TToken) =
-  writeln(stdout, $tok)
-
-proc setKeyword(L: var TLexer, tok: var TToken) =
-  var x = binaryStrSearch(keywords, toLower(tok.ident.s))
-  if x < 0: tok.xkind = pxSymbol
-  else: tok.xKind = TTokKind(x + ord(firstKeyword))
-
-proc matchUnderscoreChars(L: var TLexer, tok: var TToken, chars: TCharSet) =
-  # matches ([chars]_)*
-  var pos = L.bufpos              # use registers for pos, buf
-  var buf = L.buf
-  while true:
-    if buf[pos] in chars:
-      add(tok.literal, buf[pos])
-      inc(pos)
-    else:
-      break
-    if buf[pos] == '_':
-      add(tok.literal, '_')
-      inc(pos)
-  L.bufPos = pos
-
-proc isFloatLiteral(s: string): bool =
-  for i in countup(0, len(s)-1):
-    if s[i] in {'.', 'e', 'E'}:
-      return true
-
-proc getNumber2(L: var TLexer, tok: var TToken) =
-  var pos = L.bufpos + 1 # skip %
-  if not (L.buf[pos] in {'0'..'1'}):
-    # BUGFIX for %date%
-    tok.xkind = pxInvalid
-    add(tok.literal, '%')
-    inc(L.bufpos)
-    return
-  tok.base = base2
-  var xi: BiggestInt = 0
-  var bits = 0
-  while true:
-    case L.buf[pos]
-    of 'A'..'Z', 'a'..'z', '2'..'9', '.':
-      lexMessage(L, errInvalidNumber)
-      inc(pos)
-    of '_':
-      inc(pos)
-    of '0', '1':
-      xi = `shl`(xi, 1) or (ord(L.buf[pos]) - ord('0'))
-      inc(pos)
-      inc(bits)
-    else: break
-  tok.iNumber = xi
-  if (bits > 32): tok.xkind = pxInt64Lit
-  else: tok.xkind = pxIntLit
-  L.bufpos = pos
-
-proc getNumber16(L: var TLexer, tok: var TToken) =
-  var pos = L.bufpos + 1          # skip $
-  tok.base = base16
-  var xi: BiggestInt = 0
-  var bits = 0
-  while true:
-    case L.buf[pos]
-    of 'G'..'Z', 'g'..'z', '.':
-      lexMessage(L, errInvalidNumber)
-      inc(pos)
-    of '_': inc(pos)
-    of '0'..'9':
-      xi = `shl`(xi, 4) or (ord(L.buf[pos]) - ord('0'))
-      inc(pos)
-      inc(bits, 4)
-    of 'a'..'f':
-      xi = `shl`(xi, 4) or (ord(L.buf[pos]) - ord('a') + 10)
-      inc(pos)
-      inc(bits, 4)
-    of 'A'..'F':
-      xi = `shl`(xi, 4) or (ord(L.buf[pos]) - ord('A') + 10)
-      inc(pos)
-      inc(bits, 4)
-    else: break
-  tok.iNumber = xi
-  if (bits > 32):
-    tok.xkind = pxInt64Lit
-  else:
-    tok.xkind = pxIntLit
-  L.bufpos = pos
-
-proc getNumber10(L: var TLexer, tok: var TToken) =
-  tok.base = base10
-  matchUnderscoreChars(L, tok, {'0'..'9'})
-  if (L.buf[L.bufpos] == '.') and (L.buf[L.bufpos + 1] in {'0'..'9'}):
-    add(tok.literal, '.')
-    inc(L.bufpos)
-    matchUnderscoreChars(L, tok, {'e', 'E', '+', '-', '0'..'9'})
-  try:
-    if isFloatLiteral(tok.literal):
-      tok.fnumber = parseFloat(tok.literal)
-      tok.xkind = pxFloatLit
-    else:
-      tok.iNumber = parseInt(tok.literal)
-      if (tok.iNumber < low(int32)) or (tok.iNumber > high(int32)):
-        tok.xkind = pxInt64Lit
-      else:
-        tok.xkind = pxIntLit
-  except EInvalidValue:
-    lexMessage(L, errInvalidNumber, tok.literal)
-  except EOverflow:
-    lexMessage(L, errNumberOutOfRange, tok.literal)
-
-proc handleCRLF(L: var TLexer, pos: int): int =
-  case L.buf[pos]
-  of CR: result = nimlexbase.handleCR(L, pos)
-  of LF: result = nimlexbase.handleLF(L, pos)
-  else: result = pos
-
-proc getString(L: var TLexer, tok: var TToken) =
-  var xi: int
-  var pos = L.bufPos
-  var buf = L.buf
-  while true:
-    if buf[pos] == '\'':
-      inc(pos)
-      while true:
-        case buf[pos]
-        of CR, LF, nimlexbase.EndOfFile:
-          lexMessage(L, errClosingQuoteExpected)
-          break
-        of '\'':
-          inc(pos)
-          if buf[pos] == '\'':
-            inc(pos)
-            add(tok.literal, '\'')
-          else:
-            break
-        else:
-          add(tok.literal, buf[pos])
-          inc(pos)
-    elif buf[pos] == '#':
-      inc(pos)
-      xi = 0
-      case buf[pos]
-      of '$':
-        inc(pos)
-        xi = 0
-        while true:
-          case buf[pos]
-          of '0'..'9': xi = (xi shl 4) or (ord(buf[pos]) - ord('0'))
-          of 'a'..'f': xi = (xi shl 4) or (ord(buf[pos]) - ord('a') + 10)
-          of 'A'..'F': xi = (xi shl 4) or (ord(buf[pos]) - ord('A') + 10)
-          else: break
-          inc(pos)
-      of '0'..'9':
-        xi = 0
-        while buf[pos] in {'0'..'9'}:
-          xi = (xi * 10) + (ord(buf[pos]) - ord('0'))
-          inc(pos)
-      else: lexMessage(L, errInvalidCharacterConstant)
-      if (xi <= 255): add(tok.literal, chr(xi))
-      else: lexMessage(L, errInvalidCharacterConstant)
-    else:
-      break
-  tok.xkind = pxStrLit
-  L.bufpos = pos
-
-proc getSymbol(L: var TLexer, tok: var TToken) =
-  var h: THash = 0
-  var pos = L.bufpos
-  var buf = L.buf
-  while true:
-    var c = buf[pos]
-    case c
-    of 'a'..'z', '0'..'9', '\x80'..'\xFF':
-      h = h +% ord(c)
-      h = h +% h shl 10
-      h = h xor (h shr 6)
-    of 'A'..'Z':
-      c = chr(ord(c) + (ord('a') - ord('A'))) # toLower()
-      h = h +% ord(c)
-      h = h +% h shl 10
-      h = h xor (h shr 6)
-    of '_': discard
-    else: break
-    inc(pos)
-  h = h +% h shl 3
-  h = h xor (h shr 11)
-  h = h +% h shl 15
-  tok.ident = getIdent(addr(L.buf[L.bufpos]), pos - L.bufpos, h)
-  L.bufpos = pos
-  setKeyword(L, tok)
-
-proc scanLineComment(L: var TLexer, tok: var TToken) =
-  var pos = L.bufpos
-  var buf = L.buf
-  # a comment ends if the next line does not start with the // on the same
-  # column after only whitespace
-  tok.xkind = pxComment
-  var col = getColNumber(L, pos)
-  while true:
-    inc(pos, 2)               # skip //
-    add(tok.literal, '#')
-    while not (buf[pos] in {CR, LF, nimlexbase.EndOfFile}):
-      add(tok.literal, buf[pos])
-      inc(pos)
-    pos = handleCRLF(L, pos)
-    buf = L.buf
-    var indent = 0
-    while buf[pos] == ' ':
-      inc(pos)
-      inc(indent)
-    if (col == indent) and (buf[pos] == '/') and (buf[pos + 1] == '/'):
-      tok.literal = tok.literal & "\n"
-    else:
-      break
-  L.bufpos = pos
-
-proc scanCurlyComment(L: var TLexer, tok: var TToken) =
-  var pos = L.bufpos
-  var buf = L.buf
-  tok.literal = "#"
-  tok.xkind = pxComment
-  while true:
-    case buf[pos]
-    of CR, LF:
-      pos = handleCRLF(L, pos)
-      buf = L.buf
-      add(tok.literal, "\n#")
-    of '}':
-      inc(pos)
-      break
-    of nimlexbase.EndOfFile: lexMessage(L, errTokenExpected, "}")
-    else:
-      add(tok.literal, buf[pos])
-      inc(pos)
-  L.bufpos = pos
-
-proc scanStarComment(L: var TLexer, tok: var TToken) =
-  var pos = L.bufpos
-  var buf = L.buf
-  tok.literal = "#"
-  tok.xkind = pxComment
-  while true:
-    case buf[pos]
-    of CR, LF:
-      pos = handleCRLF(L, pos)
-      buf = L.buf
-      add(tok.literal, "\n#")
-    of '*':
-      inc(pos)
-      if buf[pos] == ')':
-        inc(pos)
-        break
-      else:
-        add(tok.literal, '*')
-    of nimlexbase.EndOfFile:
-      lexMessage(L, errTokenExpected, "*)")
-    else:
-      add(tok.literal, buf[pos])
-      inc(pos)
-  L.bufpos = pos
-
-proc skip(L: var TLexer, tok: var TToken) =
-  var pos = L.bufpos
-  var buf = L.buf
-  while true:
-    case buf[pos]
-    of ' ', Tabulator:
-      inc(pos)                # newline is special:
-    of CR, LF:
-      pos = handleCRLF(L, pos)
-      buf = L.buf
-    else:
-      break                   # EndOfFile also leaves the loop
-  L.bufpos = pos
-
-proc getTok(L: var TLexer, tok: var TToken) =
-  tok.xkind = pxInvalid
-  fillToken(tok)
-  skip(L, tok)
-  var c = L.buf[L.bufpos]
-  if c in SymStartChars:
-    getSymbol(L, tok)
-  elif c in {'0'..'9'}:
-    getNumber10(L, tok)
-  else:
-    case c
-    of ';':
-      tok.xkind = pxSemicolon
-      inc(L.bufpos)
-    of '/':
-      if L.buf[L.bufpos + 1] == '/':
-        scanLineComment(L, tok)
-      else:
-        tok.xkind = pxSlash
-        inc(L.bufpos)
-    of ',':
-      tok.xkind = pxComma
-      inc(L.bufpos)
-    of '(':
-      inc(L.bufpos)
-      if (L.buf[L.bufPos] == '*'):
-        if (L.buf[L.bufPos + 1] == '$'):
-          inc(L.bufpos, 2)
-          skip(L, tok)
-          getSymbol(L, tok)
-          tok.xkind = pxStarDirLe
-        else:
-          inc(L.bufpos)
-          scanStarComment(L, tok)
-      else:
-        tok.xkind = pxParLe
-    of '*':
-      inc(L.bufpos)
-      if L.buf[L.bufpos] == ')':
-        inc(L.bufpos)
-        tok.xkind = pxStarDirRi
-      else:
-        tok.xkind = pxStar
-    of ')':
-      tok.xkind = pxParRi
-      inc(L.bufpos)
-    of '[':
-      inc(L.bufpos)
-      tok.xkind = pxBracketLe
-    of ']':
-      inc(L.bufpos)
-      tok.xkind = pxBracketRi
-    of '.':
-      inc(L.bufpos)
-      if L.buf[L.bufpos] == '.':
-        tok.xkind = pxDotDot
-        inc(L.bufpos)
-      else:
-        tok.xkind = pxDot
-    of '{':
-      inc(L.bufpos)
-      case L.buf[L.bufpos]
-      of '$':
-        inc(L.bufpos)
-        skip(L, tok)
-        getSymbol(L, tok)
-        tok.xkind = pxCurlyDirLe
-      of '&':
-        inc(L.bufpos)
-        tok.xkind = pxAmp
-      of '%':
-        inc(L.bufpos)
-        tok.xkind = pxPer
-      of '@':
-        inc(L.bufpos)
-        tok.xkind = pxCommand
-      else: scanCurlyComment(L, tok)
-    of '+':
-      tok.xkind = pxPlus
-      inc(L.bufpos)
-    of '-':
-      tok.xkind = pxMinus
-      inc(L.bufpos)
-    of ':':
-      inc(L.bufpos)
-      if L.buf[L.bufpos] == '=':
-        inc(L.bufpos)
-        tok.xkind = pxAsgn
-      else:
-        tok.xkind = pxColon
-    of '<':
-      inc(L.bufpos)
-      if L.buf[L.bufpos] == '>':
-        inc(L.bufpos)
-        tok.xkind = pxNeq
-      elif L.buf[L.bufpos] == '=':
-        inc(L.bufpos)
-        tok.xkind = pxLe
-      else:
-        tok.xkind = pxLt
-    of '>':
-      inc(L.bufpos)
-      if L.buf[L.bufpos] == '=':
-        inc(L.bufpos)
-        tok.xkind = pxGe
-      else:
-        tok.xkind = pxGt
-    of '=':
-      tok.xkind = pxEquals
-      inc(L.bufpos)
-    of '@':
-      tok.xkind = pxAt
-      inc(L.bufpos)
-    of '^':
-      tok.xkind = pxHat
-      inc(L.bufpos)
-    of '}':
-      tok.xkind = pxCurlyDirRi
-      inc(L.bufpos)
-    of '\'', '#':
-      getString(L, tok)
-    of '$':
-      getNumber16(L, tok)
-    of '%':
-      getNumber2(L, tok)
-    of nimlexbase.EndOfFile:
-      tok.xkind = pxEof
-    else:
-      tok.literal = c & ""
-      tok.xkind = pxInvalid
-      lexMessage(L, errInvalidToken, c & " (\\" & $(ord(c)) & ')')
-      inc(L.bufpos)
diff --git a/compiler/pas2nim/pasparse.nim b/compiler/pas2nim/pasparse.nim
deleted file mode 100644
index a6f8363f6..000000000
--- a/compiler/pas2nim/pasparse.nim
+++ /dev/null
@@ -1,1513 +0,0 @@
-#
-#
-#      Pas2nim - Pascal to Nimrod source converter
-#        (c) Copyright 2012 Andreas Rumpf
-#
-#    See the file "copying.txt", included in this
-#    distribution, for details about the copyright.
-#
-
-# This module implements the parser of the Pascal variant Nimrod is written in.
-# It transfers a Pascal module into a Nimrod AST. Then the renderer can be
-# used to convert the AST to its text representation.
-
-import 
-  os, llstream, paslex, idents, strutils, ast, astalgo, msgs, options
-
-type 
-  TSection = enum 
-    seImplementation, seInterface
-  TContext = enum 
-    conExpr, conStmt, conTypeDesc
-  TParserFlag* = enum
-    pfRefs,             ## use "ref" instead of "ptr" for Pascal's ^typ
-    pfMoreReplacements, ## use more than the default replacements
-    pfImportBlackList   ## use import blacklist
-  TParser*{.final.} = object
-    section: TSection
-    inParamList: bool
-    context: TContext     # needed for the @emit command
-    lastVarSection: PNode
-    lex: TLexer
-    tok: TToken
-    repl: TIdTable           # replacements
-    flags: set[TParserFlag]
-  
-  TReplaceTuple* = array[0..1, string]
-
-const 
-  ImportBlackList*: array[1..3, string] = ["nsystem", "sysutils", "charsets"]
-  stdReplacements*: array[1..19, TReplaceTuple] = [["include", "incl"], 
-    ["exclude", "excl"], ["pchar", "cstring"], ["assignfile", "open"], 
-    ["integer", "int"], ["longword", "int32"], ["cardinal", "int"], 
-    ["boolean", "bool"], ["shortint", "int8"], ["smallint", "int16"], 
-    ["longint", "int32"], ["byte", "int8"], ["word", "int16"], 
-    ["single", "float32"], ["double", "float64"], ["real", "float"], 
-    ["length", "len"], ["len", "length"], ["setlength", "setlen"]]
-  nimReplacements*: array[1..35, TReplaceTuple] = [["nimread", "read"], 
-    ["nimwrite", "write"], ["nimclosefile", "close"], ["closefile", "close"], 
-    ["openfile", "open"], ["nsystem", "system"], ["ntime", "times"], 
-    ["nos", "os"], ["nmath", "math"], ["ncopy", "copy"], ["addChar", "add"], 
-    ["halt", "quit"], ["nobject", "TObject"], ["eof", "EndOfFile"], 
-    ["input", "stdin"], ["output", "stdout"], ["addu", "`+%`"], 
-    ["subu", "`-%`"], ["mulu", "`*%`"], ["divu", "`/%`"], ["modu", "`%%`"], 
-    ["ltu", "`<%`"], ["leu", "`<=%`"], ["shlu", "`shl`"], ["shru", "`shr`"], 
-    ["assigned", "not isNil"], ["eintoverflow", "EOverflow"], ["format", "`%`"], 
-    ["snil", "nil"], ["tostringf", "$"], ["ttextfile", "tfile"], 
-    ["tbinaryfile", "tfile"], ["strstart", "0"], ["nl", "\"\\n\""],
-    ["tostring", "$"]]
-
-proc parseUnit*(p: var TParser): PNode
-proc openParser*(p: var TParser, filename: string, inputStream: PLLStream,
-                 flags: set[TParserFlag] = {})
-proc closeParser*(p: var TParser)
-proc exSymbol*(n: var PNode)
-proc fixRecordDef*(n: var PNode)
-  # XXX: move these two to an auxiliary module
-
-# implementation
-
-proc openParser(p: var TParser, filename: string, 
-                inputStream: PLLStream, flags: set[TParserFlag] = {}) = 
-  openLexer(p.lex, filename, inputStream)
-  initIdTable(p.repl)
-  for i in countup(low(stdReplacements), high(stdReplacements)): 
-    idTablePut(p.repl, getIdent(stdReplacements[i][0]), 
-               getIdent(stdReplacements[i][1]))
-  if pfMoreReplacements in flags: 
-    for i in countup(low(nimReplacements), high(nimReplacements)): 
-      idTablePut(p.repl, getIdent(nimReplacements[i][0]), 
-                 getIdent(nimReplacements[i][1]))
-  p.flags = flags
-  
-proc closeParser(p: var TParser) = closeLexer(p.lex)
-proc getTok(p: var TParser) = getTok(p.lex, p.tok)
-
-proc parMessage(p: TParser, msg: TMsgKind, arg = "") = 
-  lexMessage(p.lex, msg, arg)
-
-proc parLineInfo(p: TParser): TLineInfo = 
-  result = getLineInfo(p.lex)
-
-proc skipCom(p: var TParser, n: PNode) = 
-  while p.tok.xkind == pxComment: 
-    if (n != nil): 
-      if n.comment == nil: n.comment = p.tok.literal
-      else: add(n.comment, "\n" & p.tok.literal)
-    else: 
-      parMessage(p, warnCommentXIgnored, p.tok.literal)
-    getTok(p)
-
-proc expectIdent(p: TParser) = 
-  if p.tok.xkind != pxSymbol: 
-    lexMessage(p.lex, errIdentifierExpected, $(p.tok))
-  
-proc eat(p: var TParser, xkind: TTokKind) = 
-  if p.tok.xkind == xkind: getTok(p)
-  else: lexMessage(p.lex, errTokenExpected, tokKindToStr(xkind))
-  
-proc opt(p: var TParser, xkind: TTokKind) = 
-  if p.tok.xkind == xkind: getTok(p)
-  
-proc newNodeP(kind: TNodeKind, p: TParser): PNode = 
-  result = newNodeI(kind, getLineInfo(p.lex))
-
-proc newIntNodeP(kind: TNodeKind, intVal: BiggestInt, p: TParser): PNode = 
-  result = newNodeP(kind, p)
-  result.intVal = intVal
-
-proc newFloatNodeP(kind: TNodeKind, floatVal: BiggestFloat, 
-                   p: TParser): PNode = 
-  result = newNodeP(kind, p)
-  result.floatVal = floatVal
-
-proc newStrNodeP(kind: TNodeKind, strVal: string, p: TParser): PNode = 
-  result = newNodeP(kind, p)
-  result.strVal = strVal
-
-proc newIdentNodeP(ident: PIdent, p: TParser): PNode = 
-  result = newNodeP(nkIdent, p)
-  result.ident = ident
-
-proc createIdentNodeP(ident: PIdent, p: TParser): PNode = 
-  result = newNodeP(nkIdent, p)
-  var x = PIdent(idTableGet(p.repl, ident))
-  if x != nil: result.ident = x
-  else: result.ident = ident
-  
-proc parseExpr(p: var TParser): PNode
-proc parseStmt(p: var TParser): PNode
-proc parseTypeDesc(p: var TParser, definition: PNode = nil): PNode
-
-proc parseEmit(p: var TParser, definition: PNode): PNode = 
-  getTok(p)                   # skip 'emit'
-  result = ast.emptyNode
-  if p.tok.xkind != pxCurlyDirRi: 
-    case p.context
-    of conExpr: 
-      result = parseExpr(p)
-    of conStmt: 
-      result = parseStmt(p)
-      if p.tok.xkind != pxCurlyDirRi: 
-        var a = result
-        result = newNodeP(nkStmtList, p)
-        addSon(result, a)
-        while p.tok.xkind != pxCurlyDirRi: 
-          addSon(result, parseStmt(p))
-    of conTypeDesc: 
-      result = parseTypeDesc(p, definition)
-  eat(p, pxCurlyDirRi)
-
-proc parseCommand(p: var TParser, definition: PNode = nil): PNode = 
-  result = ast.emptyNode
-  getTok(p)
-  if p.tok.ident.id == getIdent("discard").id: 
-    result = newNodeP(nkDiscardStmt, p)
-    getTok(p)
-    eat(p, pxCurlyDirRi)
-    addSon(result, parseExpr(p))
-  elif p.tok.ident.id == getIdent("set").id: 
-    getTok(p)
-    eat(p, pxCurlyDirRi)
-    result = parseExpr(p)
-    if result.kind == nkEmpty: internalError("emptyNode modified")
-    result.kind = nkCurly
-  elif p.tok.ident.id == getIdent("cast").id: 
-    getTok(p)
-    eat(p, pxCurlyDirRi)
-    var a = parseExpr(p)
-    if (a.kind == nkCall) and (sonsLen(a) == 2): 
-      result = newNodeP(nkCast, p)
-      addSon(result, a.sons[0])
-      addSon(result, a.sons[1])
-    else: 
-      parMessage(p, errInvalidDirectiveX, $p.tok)
-      result = a
-  elif p.tok.ident.id == getIdent("emit").id: 
-    result = parseEmit(p, definition)
-  elif p.tok.ident.id == getIdent("ignore").id: 
-    getTok(p)
-    eat(p, pxCurlyDirRi)
-    while true: 
-      case p.tok.xkind
-      of pxEof: 
-        parMessage(p, errTokenExpected, "{@emit}")
-      of pxCommand: 
-        getTok(p)
-        if p.tok.ident.id == getIdent("emit").id: 
-          result = parseEmit(p, definition)
-          break 
-        else: 
-          while (p.tok.xkind != pxCurlyDirRi) and (p.tok.xkind != pxEof): 
-            getTok(p)
-          eat(p, pxCurlyDirRi)
-      else: 
-        getTok(p)             # skip token
-  elif p.tok.ident.id == getIdent("ptr").id: 
-    result = newNodeP(nkPtrTy, p)
-    getTok(p)
-    eat(p, pxCurlyDirRi)
-  elif p.tok.ident.id == getIdent("tuple").id: 
-    result = newNodeP(nkTupleTy, p)
-    getTok(p)
-    eat(p, pxCurlyDirRi)
-  elif p.tok.ident.id == getIdent("acyclic").id: 
-    result = newIdentNodeP(p.tok.ident, p)
-    getTok(p)
-    eat(p, pxCurlyDirRi)
-  else: 
-    parMessage(p, errInvalidDirectiveX, $p.tok)
-    while true: 
-      getTok(p)
-      if p.tok.xkind == pxCurlyDirRi or p.tok.xkind == pxEof: break 
-    eat(p, pxCurlyDirRi)
-    result = ast.emptyNode
-
-proc getPrecedence(kind: TTokKind): int = 
-  case kind
-  of pxDiv, pxMod, pxStar, pxSlash, pxShl, pxShr, pxAnd: result = 5
-  of pxPlus, pxMinus, pxOr, pxXor: result = 4
-  of pxIn, pxEquals, pxLe, pxLt, pxGe, pxGt, pxNeq, pxIs: result = 3
-  else: result = -1
-  
-proc rangeExpr(p: var TParser): PNode = 
-  var a = parseExpr(p)
-  if p.tok.xkind == pxDotDot: 
-    result = newNodeP(nkRange, p)
-    addSon(result, a)
-    getTok(p)
-    skipCom(p, result)
-    addSon(result, parseExpr(p))
-  else: 
-    result = a
-  
-proc bracketExprList(p: var TParser, first: PNode): PNode = 
-  result = newNodeP(nkBracketExpr, p)
-  addSon(result, first)
-  getTok(p)
-  skipCom(p, result)
-  while true: 
-    if p.tok.xkind == pxBracketRi: 
-      getTok(p)
-      break 
-    if p.tok.xkind == pxEof: 
-      parMessage(p, errTokenExpected, tokKindToStr(pxBracketRi))
-      break 
-    var a = rangeExpr(p)
-    skipCom(p, a)
-    if p.tok.xkind == pxComma: 
-      getTok(p)
-      skipCom(p, a)
-    addSon(result, a)
-
-proc exprColonEqExpr(p: var TParser, kind: TNodeKind, 
-                     tok: TTokKind): PNode = 
-  var a = parseExpr(p)
-  if p.tok.xkind == tok: 
-    result = newNodeP(kind, p)
-    getTok(p)
-    skipCom(p, result)
-    addSon(result, a)
-    addSon(result, parseExpr(p))
-  else: 
-    result = a
-  
-proc exprListAux(p: var TParser, elemKind: TNodeKind, 
-                 endTok, sepTok: TTokKind, result: PNode) = 
-  getTok(p)
-  skipCom(p, result)
-  while true: 
-    if p.tok.xkind == endTok: 
-      getTok(p)
-      break 
-    if p.tok.xkind == pxEof: 
-      parMessage(p, errTokenExpected, tokKindToStr(endTok))
-      break 
-    var a = exprColonEqExpr(p, elemKind, sepTok)
-    skipCom(p, a)
-    if (p.tok.xkind == pxComma) or (p.tok.xkind == pxSemicolon): 
-      getTok(p)
-      skipCom(p, a)
-    addSon(result, a)
-
-proc qualifiedIdent(p: var TParser): PNode = 
-  if p.tok.xkind == pxSymbol: 
-    result = createIdentNodeP(p.tok.ident, p)
-  else: 
-    parMessage(p, errIdentifierExpected, $p.tok)
-    return ast.emptyNode
-  getTok(p)
-  skipCom(p, result)
-  if p.tok.xkind == pxDot: 
-    getTok(p)
-    skipCom(p, result)
-    if p.tok.xkind == pxSymbol: 
-      var a = result
-      result = newNodeI(nkDotExpr, a.info)
-      addSon(result, a)
-      addSon(result, createIdentNodeP(p.tok.ident, p))
-      getTok(p)
-    else: 
-      parMessage(p, errIdentifierExpected, $p.tok)
-  
-proc qualifiedIdentListAux(p: var TParser, endTok: TTokKind, 
-                           result: PNode) = 
-  getTok(p)
-  skipCom(p, result)
-  while true: 
-    if p.tok.xkind == endTok: 
-      getTok(p)
-      break 
-    if p.tok.xkind == pxEof: 
-      parMessage(p, errTokenExpected, tokKindToStr(endTok))
-      break 
-    var a = qualifiedIdent(p)
-    skipCom(p, a)
-    if p.tok.xkind == pxComma: 
-      getTok(p)
-      skipCom(p, a)
-    addSon(result, a)
-
-proc exprColonEqExprList(p: var TParser, kind, elemKind: TNodeKind, 
-                         endTok, sepTok: TTokKind): PNode = 
-  result = newNodeP(kind, p)
-  exprListAux(p, elemKind, endTok, sepTok, result)
-
-proc setBaseFlags(n: PNode, base: TNumericalBase) = 
-  case base
-  of base10: discard
-  of base2: incl(n.flags, nfBase2)
-  of base8: incl(n.flags, nfBase8)
-  of base16: incl(n.flags, nfBase16)
-  
-proc identOrLiteral(p: var TParser): PNode = 
-  case p.tok.xkind
-  of pxSymbol: 
-    result = createIdentNodeP(p.tok.ident, p)
-    getTok(p)
-  of pxIntLit: 
-    result = newIntNodeP(nkIntLit, p.tok.iNumber, p)
-    setBaseFlags(result, p.tok.base)
-    getTok(p)
-  of pxInt64Lit: 
-    result = newIntNodeP(nkInt64Lit, p.tok.iNumber, p)
-    setBaseFlags(result, p.tok.base)
-    getTok(p)
-  of pxFloatLit: 
-    result = newFloatNodeP(nkFloatLit, p.tok.fNumber, p)
-    setBaseFlags(result, p.tok.base)
-    getTok(p)
-  of pxStrLit: 
-    if len(p.tok.literal) != 1: result = newStrNodeP(nkStrLit, p.tok.literal, p)
-    else: result = newIntNodeP(nkCharLit, ord(p.tok.literal[0]), p)
-    getTok(p)
-  of pxNil: 
-    result = newNodeP(nkNilLit, p)
-    getTok(p)
-  of pxParLe: 
-    # () constructor
-    result = exprColonEqExprList(p, nkPar, nkExprColonExpr, pxParRi, pxColon)
-    #if hasSonWith(result, nkExprColonExpr) then
-    #  replaceSons(result, nkExprColonExpr, nkExprEqExpr)
-    if (sonsLen(result) > 1) and not hasSonWith(result, nkExprColonExpr): 
-      result.kind = nkBracket # is an array constructor
-  of pxBracketLe: 
-    # [] constructor
-    result = newNodeP(nkBracket, p)
-    getTok(p)
-    skipCom(p, result)
-    while (p.tok.xkind != pxBracketRi) and (p.tok.xkind != pxEof): 
-      var a = rangeExpr(p)
-      if a.kind == nkRange: 
-        result.kind = nkCurly # it is definitely a set literal
-      opt(p, pxComma)
-      skipCom(p, a)
-      assert(a != nil)
-      addSon(result, a)
-    eat(p, pxBracketRi)
-  of pxCommand: 
-    result = parseCommand(p)
-  else: 
-    parMessage(p, errExprExpected, $(p.tok))
-    getTok(p) # we must consume a token here to prevend endless loops!
-    result = ast.emptyNode
-  if result.kind != nkEmpty: skipCom(p, result)
-  
-proc primary(p: var TParser): PNode = 
-  # prefix operator?
-  if (p.tok.xkind == pxNot) or (p.tok.xkind == pxMinus) or
-      (p.tok.xkind == pxPlus): 
-    result = newNodeP(nkPrefix, p)
-    var a = newIdentNodeP(getIdent($p.tok), p)
-    addSon(result, a)
-    getTok(p)
-    skipCom(p, a)
-    addSon(result, primary(p))
-    return 
-  elif p.tok.xkind == pxAt: 
-    result = newNodeP(nkAddr, p)
-    var a = newIdentNodeP(getIdent($p.tok), p)
-    getTok(p)
-    if p.tok.xkind == pxBracketLe: 
-      result = newNodeP(nkPrefix, p)
-      addSon(result, a)
-      addSon(result, identOrLiteral(p))
-    else: 
-      addSon(result, primary(p))
-    return 
-  result = identOrLiteral(p)
-  while true: 
-    case p.tok.xkind
-    of pxParLe: 
-      var a = result
-      result = newNodeP(nkCall, p)
-      addSon(result, a)
-      exprListAux(p, nkExprEqExpr, pxParRi, pxEquals, result)
-    of pxDot: 
-      var a = result
-      result = newNodeP(nkDotExpr, p)
-      addSon(result, a)
-      getTok(p)               # skip '.'
-      skipCom(p, result)
-      if p.tok.xkind == pxSymbol: 
-        addSon(result, createIdentNodeP(p.tok.ident, p))
-        getTok(p)
-      else: 
-        parMessage(p, errIdentifierExpected, $p.tok)
-    of pxHat: 
-      var a = result
-      result = newNodeP(nkBracketExpr, p)
-      addSon(result, a)
-      getTok(p)
-    of pxBracketLe: 
-      result = bracketExprList(p, result)
-    else: break 
-  
-proc lowestExprAux(p: var TParser, v: var PNode, limit: int): TTokKind = 
-  var 
-    nextop: TTokKind
-    v2, node, opNode: PNode
-  v = primary(p) # expand while operators have priorities higher than 'limit'
-  var op = p.tok.xkind
-  var opPred = getPrecedence(op)
-  while (opPred > limit): 
-    node = newNodeP(nkInfix, p)
-    opNode = newIdentNodeP(getIdent($(p.tok)), p) # skip operator:
-    getTok(p)
-    case op
-    of pxPlus: 
-      case p.tok.xkind
-      of pxPer: 
-        getTok(p)
-        eat(p, pxCurlyDirRi)
-        opNode.ident = getIdent("+%")
-      of pxAmp: 
-        getTok(p)
-        eat(p, pxCurlyDirRi)
-        opNode.ident = getIdent("&")
-      else: 
-        discard
-    of pxMinus: 
-      if p.tok.xkind == pxPer: 
-        getTok(p)
-        eat(p, pxCurlyDirRi)
-        opNode.ident = getIdent("-%")
-    of pxEquals: 
-      opNode.ident = getIdent("==")
-    of pxNeq: 
-      opNode.ident = getIdent("!=")
-    else: 
-      discard
-    skipCom(p, opNode)        # read sub-expression with higher priority
-    nextop = lowestExprAux(p, v2, opPred)
-    addSon(node, opNode)
-    addSon(node, v)
-    addSon(node, v2)
-    v = node
-    op = nextop
-    opPred = getPrecedence(nextop)
-  result = op                 # return first untreated operator
-  
-proc fixExpr(n: PNode): PNode = 
-  result = n
-  case n.kind
-  of nkInfix: 
-    if n.sons[1].kind == nkBracket: n.sons[1].kind = nkCurly
-    if n.sons[2].kind == nkBracket: n.sons[2].kind = nkCurly
-    if (n.sons[0].kind == nkIdent): 
-      if (n.sons[0].ident.id == getIdent("+").id): 
-        if (n.sons[1].kind == nkCharLit) and (n.sons[2].kind == nkStrLit) and
-            (n.sons[2].strVal == ""): 
-          result = newStrNode(nkStrLit, chr(int(n.sons[1].intVal)) & "")
-          result.info = n.info
-          return              # do not process sons as they don't exist anymore
-        elif (n.sons[1].kind in {nkCharLit, nkStrLit}) or
-            (n.sons[2].kind in {nkCharLit, nkStrLit}): 
-          n.sons[0].ident = getIdent("&") # fix operator
-  else: 
-    discard
-  if not (n.kind in {nkEmpty..nkNilLit}): 
-    for i in countup(0, sonsLen(n) - 1): result.sons[i] = fixExpr(n.sons[i])
-  
-proc parseExpr(p: var TParser): PNode = 
-  var oldcontext = p.context
-  p.context = conExpr
-  if p.tok.xkind == pxCommand: 
-    result = parseCommand(p)
-  else: 
-    discard lowestExprAux(p, result, - 1)
-    result = fixExpr(result)
-  p.context = oldcontext
-
-proc parseExprStmt(p: var TParser): PNode = 
-  var info = parLineInfo(p)
-  var a = parseExpr(p)
-  if p.tok.xkind == pxAsgn: 
-    getTok(p)
-    skipCom(p, a)
-    var b = parseExpr(p)
-    result = newNodeI(nkAsgn, info)
-    addSon(result, a)
-    addSon(result, b)
-  else: 
-    result = a
-  
-proc inImportBlackList(ident: PIdent): bool = 
-  for i in countup(low(ImportBlackList), high(ImportBlackList)): 
-    if ident.id == getIdent(ImportBlackList[i]).id: 
-      return true
-
-proc parseUsesStmt(p: var TParser): PNode = 
-  var a: PNode
-  result = newNodeP(nkImportStmt, p)
-  getTok(p)                   # skip `import`
-  skipCom(p, result)
-  while true: 
-    case p.tok.xkind
-    of pxEof: break 
-    of pxSymbol: a = newIdentNodeP(p.tok.ident, p)
-    else: 
-      parMessage(p, errIdentifierExpected, $(p.tok))
-      break 
-    getTok(p)                 # skip identifier, string
-    skipCom(p, a)
-    if pfImportBlackList notin p.flags or not inImportBlackList(a.ident): 
-      addSon(result, createIdentNodeP(a.ident, p))
-    if p.tok.xkind == pxComma: 
-      getTok(p)
-      skipCom(p, a)
-    else: 
-      break 
-  if sonsLen(result) == 0: result = ast.emptyNode
-  
-proc parseIncludeDir(p: var TParser): PNode = 
-  result = newNodeP(nkIncludeStmt, p)
-  getTok(p)                   # skip `include`
-  var filename = ""
-  while true: 
-    case p.tok.xkind
-    of pxSymbol, pxDot, pxDotDot, pxSlash: 
-      add(filename, $p.tok)
-      getTok(p)
-    of pxStrLit: 
-      filename = p.tok.literal
-      getTok(p)
-      break 
-    of pxCurlyDirRi: 
-      break 
-    else: 
-      parMessage(p, errIdentifierExpected, $p.tok)
-      break 
-  addSon(result, newStrNodeP(nkStrLit, changeFileExt(filename, "nim"), p))
-  if filename == "config.inc": result = ast.emptyNode
-  
-proc definedExprAux(p: var TParser): PNode = 
-  result = newNodeP(nkCall, p)
-  addSon(result, newIdentNodeP(getIdent("defined"), p))
-  expectIdent(p)
-  addSon(result, createIdentNodeP(p.tok.ident, p))
-  getTok(p)
-
-proc isHandledDirective(p: TParser): bool = 
-  if p.tok.xkind in {pxCurlyDirLe, pxStarDirLe}: 
-    case toLower(p.tok.ident.s)
-    of "else", "endif": result = false
-    else: result = true
-  
-proc parseStmtList(p: var TParser): PNode = 
-  result = newNodeP(nkStmtList, p)
-  while true: 
-    case p.tok.xkind
-    of pxEof: 
-      break 
-    of pxCurlyDirLe, pxStarDirLe: 
-      if not isHandledDirective(p): break 
-    else: 
-      discard
-    addSon(result, parseStmt(p))
-  if sonsLen(result) == 1: result = result.sons[0]
-  
-proc parseIfDirAux(p: var TParser, result: PNode) = 
-  addSon(result.sons[0], parseStmtList(p))
-  if p.tok.xkind in {pxCurlyDirLe, pxStarDirLe}: 
-    var endMarker = succ(p.tok.xkind)
-    if toLower(p.tok.ident.s) == "else": 
-      var s = newNodeP(nkElse, p)
-      while p.tok.xkind != pxEof and p.tok.xkind != endMarker: getTok(p)
-      eat(p, endMarker)
-      addSon(s, parseStmtList(p))
-      addSon(result, s)
-    if p.tok.xkind in {pxCurlyDirLe, pxStarDirLe}: 
-      endMarker = succ(p.tok.xkind)
-      if toLower(p.tok.ident.s) == "endif": 
-        while p.tok.xkind != pxEof and p.tok.xkind != endMarker: getTok(p)
-        eat(p, endMarker)
-      else: 
-        parMessage(p, errXExpected, "{$endif}")
-  else: 
-    parMessage(p, errXExpected, "{$endif}")
-  
-proc parseIfdefDir(p: var TParser, endMarker: TTokKind): PNode = 
-  result = newNodeP(nkWhenStmt, p)
-  addSon(result, newNodeP(nkElifBranch, p))
-  getTok(p)
-  addSon(result.sons[0], definedExprAux(p))
-  eat(p, endMarker)
-  parseIfDirAux(p, result)
-
-proc parseIfndefDir(p: var TParser, endMarker: TTokKind): PNode = 
-  result = newNodeP(nkWhenStmt, p)
-  addSon(result, newNodeP(nkElifBranch, p))
-  getTok(p)
-  var e = newNodeP(nkCall, p)
-  addSon(e, newIdentNodeP(getIdent("not"), p))
-  addSon(e, definedExprAux(p))
-  eat(p, endMarker)
-  addSon(result.sons[0], e)
-  parseIfDirAux(p, result)
-
-proc parseIfDir(p: var TParser, endMarker: TTokKind): PNode = 
-  result = newNodeP(nkWhenStmt, p)
-  addSon(result, newNodeP(nkElifBranch, p))
-  getTok(p)
-  addSon(result.sons[0], parseExpr(p))
-  eat(p, endMarker)
-  parseIfDirAux(p, result)
-
-proc parseDirective(p: var TParser): PNode = 
-  result = ast.emptyNode
-  if not (p.tok.xkind in {pxCurlyDirLe, pxStarDirLe}): return 
-  var endMarker = succ(p.tok.xkind)
-  if p.tok.ident != nil: 
-    case toLower(p.tok.ident.s)
-    of "include": 
-      result = parseIncludeDir(p)
-      eat(p, endMarker)
-    of "if": result = parseIfDir(p, endMarker)
-    of "ifdef": result = parseIfdefDir(p, endMarker)
-    of "ifndef": result = parseIfndefDir(p, endMarker)
-    else: 
-      # skip unknown compiler directive
-      while p.tok.xkind != pxEof and p.tok.xkind != endMarker: getTok(p)
-      eat(p, endMarker)
-  else: 
-    eat(p, endMarker)
-  
-proc parseRaise(p: var TParser): PNode = 
-  result = newNodeP(nkRaiseStmt, p)
-  getTok(p)
-  skipCom(p, result)
-  if p.tok.xkind != pxSemicolon: addSon(result, parseExpr(p))
-  else: addSon(result, ast.emptyNode)
-  
-proc parseIf(p: var TParser): PNode = 
-  result = newNodeP(nkIfStmt, p)
-  while true: 
-    getTok(p)                 # skip ``if``
-    var branch = newNodeP(nkElifBranch, p)
-    skipCom(p, branch)
-    addSon(branch, parseExpr(p))
-    eat(p, pxThen)
-    skipCom(p, branch)
-    addSon(branch, parseStmt(p))
-    skipCom(p, branch)
-    addSon(result, branch)
-    if p.tok.xkind == pxElse: 
-      getTok(p)
-      if p.tok.xkind != pxIf: 
-        # ordinary else part:
-        branch = newNodeP(nkElse, p)
-        skipCom(p, result)    # BUGFIX
-        addSon(branch, parseStmt(p))
-        addSon(result, branch)
-        break 
-    else: 
-      break 
-  
-proc parseWhile(p: var TParser): PNode = 
-  result = newNodeP(nkWhileStmt, p)
-  getTok(p)
-  skipCom(p, result)
-  addSon(result, parseExpr(p))
-  eat(p, pxDo)
-  skipCom(p, result)
-  addSon(result, parseStmt(p))
-
-proc parseRepeat(p: var TParser): PNode = 
-  result = newNodeP(nkWhileStmt, p)
-  getTok(p)
-  skipCom(p, result)
-  addSon(result, newIdentNodeP(getIdent("true"), p))
-  var s = newNodeP(nkStmtList, p)
-  while p.tok.xkind != pxEof and p.tok.xkind != pxUntil: 
-    addSon(s, parseStmt(p))
-  eat(p, pxUntil)
-  var a = newNodeP(nkIfStmt, p)
-  skipCom(p, a)
-  var b = newNodeP(nkElifBranch, p)
-  var c = newNodeP(nkBreakStmt, p)
-  addSon(c, ast.emptyNode)
-  addSon(b, parseExpr(p))
-  skipCom(p, a)
-  addSon(b, c)
-  addSon(a, b)
-  if b.sons[0].kind == nkIdent and b.sons[0].ident.id == getIdent("false").id: 
-    discard
-  else: 
-    addSon(s, a)
-  addSon(result, s)
-
-proc parseCase(p: var TParser): PNode = 
-  var b: PNode
-  result = newNodeP(nkCaseStmt, p)
-  getTok(p)
-  addSon(result, parseExpr(p))
-  eat(p, pxOf)
-  skipCom(p, result)
-  while (p.tok.xkind != pxEnd) and (p.tok.xkind != pxEof): 
-    if p.tok.xkind == pxElse: 
-      b = newNodeP(nkElse, p)
-      getTok(p)
-    else: 
-      b = newNodeP(nkOfBranch, p)
-      while (p.tok.xkind != pxEof) and (p.tok.xkind != pxColon): 
-        addSon(b, rangeExpr(p))
-        opt(p, pxComma)
-        skipCom(p, b)
-      eat(p, pxColon)
-    skipCom(p, b)
-    addSon(b, parseStmt(p))
-    addSon(result, b)
-    if b.kind == nkElse: break 
-  eat(p, pxEnd)
-
-proc parseTry(p: var TParser): PNode = 
-  result = newNodeP(nkTryStmt, p)
-  getTok(p)
-  skipCom(p, result)
-  var b = newNodeP(nkStmtList, p)
-  while not (p.tok.xkind in {pxFinally, pxExcept, pxEof, pxEnd}): 
-    addSon(b, parseStmt(p))
-  addSon(result, b)
-  if p.tok.xkind == pxExcept: 
-    getTok(p)
-    while p.tok.ident.id == getIdent("on").id: 
-      b = newNodeP(nkExceptBranch, p)
-      getTok(p)
-      var e = qualifiedIdent(p)
-      if p.tok.xkind == pxColon: 
-        getTok(p)
-        e = qualifiedIdent(p)
-      addSon(b, e)
-      eat(p, pxDo)
-      addSon(b, parseStmt(p))
-      addSon(result, b)
-      if p.tok.xkind == pxCommand: discard parseCommand(p)
-    if p.tok.xkind == pxElse: 
-      b = newNodeP(nkExceptBranch, p)
-      getTok(p)
-      addSon(b, parseStmt(p))
-      addSon(result, b)
-  if p.tok.xkind == pxFinally: 
-    b = newNodeP(nkFinally, p)
-    getTok(p)
-    var e = newNodeP(nkStmtList, p)
-    while (p.tok.xkind != pxEof) and (p.tok.xkind != pxEnd): 
-      addSon(e, parseStmt(p))
-    if sonsLen(e) == 0: addSon(e, newNodeP(nkNilLit, p))
-    addSon(result, e)
-  eat(p, pxEnd)
-
-proc parseFor(p: var TParser): PNode = 
-  result = newNodeP(nkForStmt, p)
-  getTok(p)
-  skipCom(p, result)
-  expectIdent(p)
-  addSon(result, createIdentNodeP(p.tok.ident, p))
-  getTok(p)
-  eat(p, pxAsgn)
-  var a = parseExpr(p)
-  var b = ast.emptyNode
-  var c = newNodeP(nkCall, p)
-  if p.tok.xkind == pxTo: 
-    addSon(c, newIdentNodeP(getIdent("countup"), p))
-    getTok(p)
-    b = parseExpr(p)
-  elif p.tok.xkind == pxDownto: 
-    addSon(c, newIdentNodeP(getIdent("countdown"), p))
-    getTok(p)
-    b = parseExpr(p)
-  else: 
-    parMessage(p, errTokenExpected, tokKindToStr(pxTo))
-  addSon(c, a)
-  addSon(c, b)
-  eat(p, pxDo)
-  skipCom(p, result)
-  addSon(result, c)
-  addSon(result, parseStmt(p))
-
-proc parseParam(p: var TParser): PNode = 
-  var a: PNode
-  result = newNodeP(nkIdentDefs, p)
-  var v = ast.emptyNode
-  case p.tok.xkind
-  of pxConst: 
-    getTok(p)
-  of pxVar: 
-    getTok(p)
-    v = newNodeP(nkVarTy, p)
-  of pxOut: 
-    getTok(p)
-    v = newNodeP(nkVarTy, p)
-  else: 
-    discard
-  while true: 
-    case p.tok.xkind
-    of pxSymbol: a = createIdentNodeP(p.tok.ident, p)
-    of pxColon, pxEof, pxParRi, pxEquals: break 
-    else: 
-      parMessage(p, errIdentifierExpected, $p.tok)
-      return 
-    getTok(p)                 # skip identifier
-    skipCom(p, a)
-    if p.tok.xkind == pxComma: 
-      getTok(p)
-      skipCom(p, a)
-    addSon(result, a)
-  if p.tok.xkind == pxColon: 
-    getTok(p)
-    skipCom(p, result)
-    if v.kind != nkEmpty: addSon(v, parseTypeDesc(p))
-    else: v = parseTypeDesc(p)
-    addSon(result, v)
-  else: 
-    addSon(result, ast.emptyNode)
-    if p.tok.xkind != pxEquals: 
-      parMessage(p, errColonOrEqualsExpected, $p.tok)
-  if p.tok.xkind == pxEquals: 
-    getTok(p)
-    skipCom(p, result)
-    addSon(result, parseExpr(p))
-  else: 
-    addSon(result, ast.emptyNode)
-  
-proc parseParamList(p: var TParser): PNode = 
-  var a: PNode
-  result = newNodeP(nkFormalParams, p)
-  addSon(result, ast.emptyNode)         # return type
-  if p.tok.xkind == pxParLe: 
-    p.inParamList = true
-    getTok(p)
-    skipCom(p, result)
-    while true: 
-      case p.tok.xkind
-      of pxSymbol, pxConst, pxVar, pxOut: 
-        a = parseParam(p)
-      of pxParRi: 
-        getTok(p)
-        break 
-      else: 
-        parMessage(p, errTokenExpected, ")")
-        break 
-      skipCom(p, a)
-      if p.tok.xkind == pxSemicolon: 
-        getTok(p)
-        skipCom(p, a)
-      addSon(result, a)
-    p.inParamList = false
-  if p.tok.xkind == pxColon: 
-    getTok(p)
-    skipCom(p, result)
-    result.sons[0] = parseTypeDesc(p)
-
-proc parseCallingConvention(p: var TParser): PNode = 
-  result = ast.emptyNode
-  if p.tok.xkind == pxSymbol: 
-    case toLower(p.tok.ident.s)
-    of "stdcall", "cdecl", "safecall", "syscall", "inline", "fastcall": 
-      result = newNodeP(nkPragma, p)
-      addSon(result, newIdentNodeP(p.tok.ident, p))
-      getTok(p)
-      opt(p, pxSemicolon)
-    of "register": 
-      result = newNodeP(nkPragma, p)
-      addSon(result, newIdentNodeP(getIdent("fastcall"), p))
-      getTok(p)
-      opt(p, pxSemicolon)
-    else: 
-      discard
-
-proc parseRoutineSpecifiers(p: var TParser, noBody: var bool): PNode = 
-  var e: PNode
-  result = parseCallingConvention(p)
-  noBody = false
-  while p.tok.xkind == pxSymbol: 
-    case toLower(p.tok.ident.s)
-    of "assembler", "overload", "far": 
-      getTok(p)
-      opt(p, pxSemicolon)
-    of "forward": 
-      noBody = true
-      getTok(p)
-      opt(p, pxSemicolon)
-    of "importc": 
-      # This is a fake for platform module. There is no ``importc``
-      # directive in Pascal.
-      if result.kind == nkEmpty: result = newNodeP(nkPragma, p)
-      addSon(result, newIdentNodeP(getIdent("importc"), p))
-      noBody = true
-      getTok(p)
-      opt(p, pxSemicolon)
-    of "noconv": 
-      # This is a fake for platform module. There is no ``noconv``
-      # directive in Pascal.
-      if result.kind == nkEmpty: result = newNodeP(nkPragma, p)
-      addSon(result, newIdentNodeP(getIdent("noconv"), p))
-      noBody = true
-      getTok(p)
-      opt(p, pxSemicolon)
-    of "procvar": 
-      # This is a fake for the Nimrod compiler. There is no ``procvar``
-      # directive in Pascal.
-      if result.kind == nkEmpty: result = newNodeP(nkPragma, p)
-      addSon(result, newIdentNodeP(getIdent("procvar"), p))
-      getTok(p)
-      opt(p, pxSemicolon)
-    of "varargs": 
-      if result.kind == nkEmpty: result = newNodeP(nkPragma, p)
-      addSon(result, newIdentNodeP(getIdent("varargs"), p))
-      getTok(p)
-      opt(p, pxSemicolon)
-    of "external": 
-      if result.kind == nkEmpty: result = newNodeP(nkPragma, p)
-      getTok(p)
-      noBody = true
-      e = newNodeP(nkExprColonExpr, p)
-      addSon(e, newIdentNodeP(getIdent("dynlib"), p))
-      addSon(e, parseExpr(p))
-      addSon(result, e)
-      opt(p, pxSemicolon)
-      if (p.tok.xkind == pxSymbol) and
-          (p.tok.ident.id == getIdent("name").id): 
-        e = newNodeP(nkExprColonExpr, p)
-        getTok(p)
-        addSon(e, newIdentNodeP(getIdent("importc"), p))
-        addSon(e, parseExpr(p))
-        addSon(result, e)
-      else: 
-        addSon(result, newIdentNodeP(getIdent("importc"), p))
-      opt(p, pxSemicolon)
-    else: 
-      e = parseCallingConvention(p)
-      if e.kind == nkEmpty: break 
-      if result.kind == nkEmpty: result = newNodeP(nkPragma, p)
-      addSon(result, e.sons[0])
-
-proc parseRoutineType(p: var TParser): PNode = 
-  result = newNodeP(nkProcTy, p)
-  getTok(p)
-  skipCom(p, result)
-  addSon(result, parseParamList(p))
-  opt(p, pxSemicolon)
-  addSon(result, parseCallingConvention(p))
-  skipCom(p, result)
-
-proc parseEnum(p: var TParser): PNode = 
-  var a: PNode
-  result = newNodeP(nkEnumTy, p)
-  getTok(p)
-  skipCom(p, result)
-  addSon(result, ast.emptyNode) # it does not inherit from any enumeration
-  while true: 
-    case p.tok.xkind
-    of pxEof, pxParRi: break 
-    of pxSymbol: a = newIdentNodeP(p.tok.ident, p)
-    else: 
-      parMessage(p, errIdentifierExpected, $(p.tok))
-      break 
-    getTok(p)                 # skip identifier
-    skipCom(p, a)
-    if (p.tok.xkind == pxEquals) or (p.tok.xkind == pxAsgn): 
-      getTok(p)
-      skipCom(p, a)
-      var b = a
-      a = newNodeP(nkEnumFieldDef, p)
-      addSon(a, b)
-      addSon(a, parseExpr(p))
-    if p.tok.xkind == pxComma: 
-      getTok(p)
-      skipCom(p, a)
-    addSon(result, a)
-  eat(p, pxParRi)
-
-proc identVis(p: var TParser): PNode = 
-  # identifier with visability
-  var a = createIdentNodeP(p.tok.ident, p)
-  if p.section == seInterface: 
-    result = newNodeP(nkPostfix, p)
-    addSon(result, newIdentNodeP(getIdent("*"), p))
-    addSon(result, a)
-  else: 
-    result = a
-  getTok(p)
-
-type 
-  TSymbolParser = proc (p: var TParser): PNode {.nimcall.}
-
-proc rawIdent(p: var TParser): PNode = 
-  result = createIdentNodeP(p.tok.ident, p)
-  getTok(p)
-
-proc parseIdentColonEquals(p: var TParser, 
-                           identParser: TSymbolParser): PNode = 
-  var a: PNode
-  result = newNodeP(nkIdentDefs, p)
-  while true: 
-    case p.tok.xkind
-    of pxSymbol: a = identParser(p)
-    of pxColon, pxEof, pxParRi, pxEquals: break 
-    else: 
-      parMessage(p, errIdentifierExpected, $(p.tok))
-      return 
-    skipCom(p, a)
-    if p.tok.xkind == pxComma: 
-      getTok(p)
-      skipCom(p, a)
-    addSon(result, a)
-  if p.tok.xkind == pxColon: 
-    getTok(p)
-    skipCom(p, result)
-    addSon(result, parseTypeDesc(p))
-  else: 
-    addSon(result, ast.emptyNode)
-    if p.tok.xkind != pxEquals: 
-      parMessage(p, errColonOrEqualsExpected, $(p.tok))
-  if p.tok.xkind == pxEquals: 
-    getTok(p)
-    skipCom(p, result)
-    addSon(result, parseExpr(p))
-  else: 
-    addSon(result, ast.emptyNode)
-  if p.tok.xkind == pxSemicolon: 
-    getTok(p)
-    skipCom(p, result)
-
-proc parseRecordCase(p: var TParser): PNode = 
-  var b, c: PNode
-  result = newNodeP(nkRecCase, p)
-  getTok(p)
-  var a = newNodeP(nkIdentDefs, p)
-  addSon(a, rawIdent(p))
-  eat(p, pxColon)
-  addSon(a, parseTypeDesc(p))
-  addSon(a, ast.emptyNode)
-  addSon(result, a)
-  eat(p, pxOf)
-  skipCom(p, result)
-  while true: 
-    case p.tok.xkind
-    of pxEof, pxEnd: 
-      break 
-    of pxElse: 
-      b = newNodeP(nkElse, p)
-      getTok(p)
-    else: 
-      b = newNodeP(nkOfBranch, p)
-      while (p.tok.xkind != pxEof) and (p.tok.xkind != pxColon): 
-        addSon(b, rangeExpr(p))
-        opt(p, pxComma)
-        skipCom(p, b)
-      eat(p, pxColon)
-    skipCom(p, b)
-    c = newNodeP(nkRecList, p)
-    eat(p, pxParLe)
-    while (p.tok.xkind != pxParRi) and (p.tok.xkind != pxEof): 
-      addSon(c, parseIdentColonEquals(p, rawIdent))
-      opt(p, pxSemicolon)
-      skipCom(p, lastSon(c))
-    eat(p, pxParRi)
-    opt(p, pxSemicolon)
-    if sonsLen(c) > 0: skipCom(p, lastSon(c))
-    else: addSon(c, newNodeP(nkNilLit, p))
-    addSon(b, c)
-    addSon(result, b)
-    if b.kind == nkElse: break 
-  
-proc parseRecordPart(p: var TParser): PNode = 
-  result = ast.emptyNode
-  while (p.tok.xkind != pxEof) and (p.tok.xkind != pxEnd): 
-    if result.kind == nkEmpty: result = newNodeP(nkRecList, p)
-    case p.tok.xkind
-    of pxSymbol: 
-      addSon(result, parseIdentColonEquals(p, rawIdent))
-      opt(p, pxSemicolon)
-      skipCom(p, lastSon(result))
-    of pxCase: 
-      addSon(result, parseRecordCase(p))
-    of pxComment: 
-      skipCom(p, lastSon(result))
-    else: 
-      parMessage(p, errIdentifierExpected, $p.tok)
-      break
-
-proc exSymbol(n: var PNode) = 
-  case n.kind
-  of nkPostfix: 
-    discard
-  of nkPragmaExpr: 
-    exSymbol(n.sons[0])
-  of nkIdent, nkAccQuoted: 
-    var a = newNodeI(nkPostFix, n.info)
-    addSon(a, newIdentNode(getIdent("*"), n.info))
-    addSon(a, n)
-    n = a
-  else: internalError(n.info, "exSymbol(): " & $n.kind)
-  
-proc fixRecordDef(n: var PNode) = 
-  case n.kind
-  of nkRecCase: 
-    fixRecordDef(n.sons[0])
-    for i in countup(1, sonsLen(n) - 1): 
-      var length = sonsLen(n.sons[i])
-      fixRecordDef(n.sons[i].sons[length - 1])
-  of nkRecList, nkRecWhen, nkElse, nkOfBranch, nkElifBranch, nkObjectTy: 
-    for i in countup(0, sonsLen(n) - 1): fixRecordDef(n.sons[i])
-  of nkIdentDefs: 
-    for i in countup(0, sonsLen(n) - 3): exSymbol(n.sons[i])
-  of nkNilLit, nkEmpty: discard
-  else: internalError(n.info, "fixRecordDef(): " & $n.kind)
-  
-proc addPragmaToIdent(ident: var PNode, pragma: PNode) = 
-  var pragmasNode: PNode
-  if ident.kind != nkPragmaExpr: 
-    pragmasNode = newNodeI(nkPragma, ident.info)
-    var e = newNodeI(nkPragmaExpr, ident.info)
-    addSon(e, ident)
-    addSon(e, pragmasNode)
-    ident = e
-  else: 
-    pragmasNode = ident.sons[1]
-    if pragmasNode.kind != nkPragma: 
-      internalError(ident.info, "addPragmaToIdent")
-  addSon(pragmasNode, pragma)
-
-proc parseRecordBody(p: var TParser, result, definition: PNode) = 
-  skipCom(p, result)
-  var a = parseRecordPart(p)
-  if result.kind != nkTupleTy: fixRecordDef(a)
-  addSon(result, a)
-  eat(p, pxEnd)
-  case p.tok.xkind
-  of pxSymbol: 
-    if p.tok.ident.id == getIdent("acyclic").id: 
-      if definition != nil: 
-        addPragmaToIdent(definition.sons[0], newIdentNodeP(p.tok.ident, p))
-      else: 
-        internalError(result.info, "anonymous record is not supported")
-      getTok(p)
-    else: 
-      internalError(result.info, "parseRecordBody")
-  of pxCommand: 
-    if definition != nil: addPragmaToIdent(definition.sons[0], parseCommand(p))
-    else: internalError(result.info, "anonymous record is not supported")
-  else: 
-    discard
-  opt(p, pxSemicolon)
-  skipCom(p, result)
-
-proc parseRecordOrObject(p: var TParser, kind: TNodeKind, 
-                         definition: PNode): PNode = 
-  result = newNodeP(kind, p)
-  getTok(p)
-  addSon(result, ast.emptyNode)
-  if p.tok.xkind == pxParLe: 
-    var a = newNodeP(nkOfInherit, p)
-    getTok(p)
-    addSon(a, parseTypeDesc(p))
-    addSon(result, a)
-    eat(p, pxParRi)
-  else: 
-    addSon(result, ast.emptyNode)
-  parseRecordBody(p, result, definition)
-
-proc parseTypeDesc(p: var TParser, definition: PNode = nil): PNode = 
-  var oldcontext = p.context
-  p.context = conTypeDesc
-  if p.tok.xkind == pxPacked: getTok(p)
-  case p.tok.xkind
-  of pxCommand: 
-    result = parseCommand(p, definition)
-  of pxProcedure, pxFunction: 
-    result = parseRoutineType(p)
-  of pxRecord: 
-    getTok(p)
-    if p.tok.xkind == pxCommand: 
-      result = parseCommand(p)
-      if result.kind != nkTupleTy: internalError(result.info, "parseTypeDesc")
-      parseRecordBody(p, result, definition)
-      var a = lastSon(result)     # embed nkRecList directly into nkTupleTy
-      for i in countup(0, sonsLen(a) - 1): 
-        if i == 0: result.sons[sonsLen(result) - 1] = a.sons[0]
-        else: addSon(result, a.sons[i])
-    else: 
-      result = newNodeP(nkObjectTy, p)
-      addSon(result, ast.emptyNode)
-      addSon(result, ast.emptyNode)
-      parseRecordBody(p, result, definition)
-      if definition != nil: 
-        addPragmaToIdent(definition.sons[0], newIdentNodeP(getIdent("final"), p))
-      else: 
-        internalError(result.info, "anonymous record is not supported")
-  of pxObject: result = parseRecordOrObject(p, nkObjectTy, definition)
-  of pxParLe: result = parseEnum(p)
-  of pxArray: 
-    result = newNodeP(nkBracketExpr, p)
-    getTok(p)
-    if p.tok.xkind == pxBracketLe: 
-      addSon(result, newIdentNodeP(getIdent("array"), p))
-      getTok(p)
-      addSon(result, rangeExpr(p))
-      eat(p, pxBracketRi)
-    else: 
-      if p.inParamList: addSon(result, newIdentNodeP(getIdent("openarray"), p))
-      else: addSon(result, newIdentNodeP(getIdent("seq"), p))
-    eat(p, pxOf)
-    addSon(result, parseTypeDesc(p))
-  of pxSet: 
-    result = newNodeP(nkBracketExpr, p)
-    getTok(p)
-    eat(p, pxOf)
-    addSon(result, newIdentNodeP(getIdent("set"), p))
-    addSon(result, parseTypeDesc(p))
-  of pxHat: 
-    getTok(p)
-    if p.tok.xkind == pxCommand: result = parseCommand(p)
-    elif pfRefs in p.flags: result = newNodeP(nkRefTy, p)
-    else: result = newNodeP(nkPtrTy, p)
-    addSon(result, parseTypeDesc(p))
-  of pxType: 
-    getTok(p)
-    result = parseTypeDesc(p)
-  else: 
-    var a = primary(p)
-    if p.tok.xkind == pxDotDot: 
-      result = newNodeP(nkBracketExpr, p)
-      var r = newNodeP(nkRange, p)
-      addSon(result, newIdentNodeP(getIdent("range"), p))
-      getTok(p)
-      addSon(r, a)
-      addSon(r, parseExpr(p))
-      addSon(result, r)
-    else: 
-      result = a
-  p.context = oldcontext
-
-proc parseTypeDef(p: var TParser): PNode = 
-  result = newNodeP(nkTypeDef, p)
-  addSon(result, identVis(p))
-  addSon(result, ast.emptyNode)         # generic params
-  if p.tok.xkind == pxEquals: 
-    getTok(p)
-    skipCom(p, result)
-    addSon(result, parseTypeDesc(p, result))
-  else: 
-    addSon(result, ast.emptyNode)
-  if p.tok.xkind == pxSemicolon: 
-    getTok(p)
-    skipCom(p, result)
-
-proc parseTypeSection(p: var TParser): PNode = 
-  result = newNodeP(nkTypeSection, p)
-  getTok(p)
-  skipCom(p, result)
-  while p.tok.xkind == pxSymbol: 
-    addSon(result, parseTypeDef(p))
-
-proc parseConstant(p: var TParser): PNode = 
-  result = newNodeP(nkConstDef, p)
-  addSon(result, identVis(p))
-  if p.tok.xkind == pxColon: 
-    getTok(p)
-    skipCom(p, result)
-    addSon(result, parseTypeDesc(p))
-  else: 
-    addSon(result, ast.emptyNode)
-    if p.tok.xkind != pxEquals: 
-      parMessage(p, errColonOrEqualsExpected, $(p.tok))
-  if p.tok.xkind == pxEquals: 
-    getTok(p)
-    skipCom(p, result)
-    addSon(result, parseExpr(p))
-  else: 
-    addSon(result, ast.emptyNode)
-  if p.tok.xkind == pxSemicolon: 
-    getTok(p)
-    skipCom(p, result)
-
-proc parseConstSection(p: var TParser): PNode = 
-  result = newNodeP(nkConstSection, p)
-  getTok(p)
-  skipCom(p, result)
-  while p.tok.xkind == pxSymbol: 
-    addSon(result, parseConstant(p))
-
-proc parseVar(p: var TParser): PNode = 
-  result = newNodeP(nkVarSection, p)
-  getTok(p)
-  skipCom(p, result)
-  while p.tok.xkind == pxSymbol:
-    addSon(result, parseIdentColonEquals(p, identVis))
-  p.lastVarSection = result
-
-proc parseRoutine(p: var TParser): PNode = 
-  var noBody: bool
-  result = newNodeP(nkProcDef, p)
-  getTok(p)
-  skipCom(p, result)
-  expectIdent(p)
-  addSon(result, identVis(p))
-  # patterns, generic parameters:
-  addSon(result, ast.emptyNode)
-  addSon(result, ast.emptyNode)   
-  addSon(result, parseParamList(p))
-  opt(p, pxSemicolon)
-  addSon(result, parseRoutineSpecifiers(p, noBody))
-  addSon(result, ast.emptyNode)
-  if (p.section == seInterface) or noBody: 
-    addSon(result, ast.emptyNode)
-  else: 
-    var stmts = newNodeP(nkStmtList, p)
-    while true: 
-      case p.tok.xkind
-      of pxVar: addSon(stmts, parseVar(p))
-      of pxConst: addSon(stmts, parseConstSection(p))
-      of pxType: addSon(stmts, parseTypeSection(p))
-      of pxComment: skipCom(p, result)
-      of pxBegin: break 
-      else: 
-        parMessage(p, errTokenExpected, "begin")
-        break 
-    var a = parseStmt(p)
-    for i in countup(0, sonsLen(a) - 1): addSon(stmts, a.sons[i])
-    addSon(result, stmts)
-
-proc fixExit(p: var TParser, n: PNode): bool = 
-  if (p.tok.ident.id == getIdent("exit").id): 
-    var length = sonsLen(n)
-    if (length <= 0): return 
-    var a = n.sons[length-1]
-    if (a.kind == nkAsgn) and (a.sons[0].kind == nkIdent) and
-        (a.sons[0].ident.id == getIdent("result").id): 
-      delSon(a, 0)
-      a.kind = nkReturnStmt
-      result = true
-      getTok(p)
-      opt(p, pxSemicolon)
-      skipCom(p, a)
-
-proc fixVarSection(p: var TParser, counter: PNode) = 
-  if p.lastVarSection == nil: return 
-  assert(counter.kind == nkIdent)
-  for i in countup(0, sonsLen(p.lastVarSection) - 1): 
-    var v = p.lastVarSection.sons[i]
-    for j in countup(0, sonsLen(v) - 3): 
-      if v.sons[j].ident.id == counter.ident.id: 
-        delSon(v, j)
-        if sonsLen(v) <= 2: 
-          delSon(p.lastVarSection, i)
-        return 
-
-proc exSymbols(n: PNode) = 
-  case n.kind
-  of nkEmpty..nkNilLit: discard
-  of nkProcDef..nkIteratorDef: exSymbol(n.sons[namePos])
-  of nkWhenStmt, nkStmtList: 
-    for i in countup(0, sonsLen(n) - 1): exSymbols(n.sons[i])
-  of nkVarSection, nkConstSection: 
-    for i in countup(0, sonsLen(n) - 1): exSymbol(n.sons[i].sons[0])
-  of nkTypeSection: 
-    for i in countup(0, sonsLen(n) - 1): 
-      exSymbol(n.sons[i].sons[0])
-      if n.sons[i].sons[2].kind == nkObjectTy: 
-        fixRecordDef(n.sons[i].sons[2])
-  else: discard
-
-proc parseBegin(p: var TParser, result: PNode) = 
-  getTok(p)
-  while true: 
-    case p.tok.xkind
-    of pxComment: addSon(result, parseStmt(p))
-    of pxSymbol: 
-      if not fixExit(p, result): addSon(result, parseStmt(p))
-    of pxEnd: 
-      getTok(p)
-      break 
-    of pxSemicolon: getTok(p)
-    of pxEof: parMessage(p, errExprExpected)
-    else: 
-      var a = parseStmt(p)
-      if a.kind != nkEmpty: addSon(result, a)
-  if sonsLen(result) == 0: addSon(result, newNodeP(nkNilLit, p))
-  
-proc parseStmt(p: var TParser): PNode = 
-  var oldcontext = p.context
-  p.context = conStmt
-  result = ast.emptyNode
-  case p.tok.xkind
-  of pxBegin: 
-    result = newNodeP(nkStmtList, p)
-    parseBegin(p, result)
-  of pxCommand: result = parseCommand(p)
-  of pxCurlyDirLe, pxStarDirLe: 
-    if isHandledDirective(p): result = parseDirective(p)
-  of pxIf: result = parseIf(p)
-  of pxWhile: result = parseWhile(p)
-  of pxRepeat: result = parseRepeat(p)
-  of pxCase: result = parseCase(p)
-  of pxTry: result = parseTry(p)
-  of pxProcedure, pxFunction: result = parseRoutine(p)
-  of pxType: result = parseTypeSection(p)
-  of pxConst: result = parseConstSection(p)
-  of pxVar: result = parseVar(p)
-  of pxFor: 
-    result = parseFor(p)
-    fixVarSection(p, result.sons[0])
-  of pxRaise: result = parseRaise(p)
-  of pxUses: result = parseUsesStmt(p)
-  of pxProgram, pxUnit, pxLibrary: 
-    # skip the pointless header
-    while not (p.tok.xkind in {pxSemicolon, pxEof}): getTok(p)
-    getTok(p)
-  of pxInitialization: getTok(p) # just skip the token
-  of pxImplementation: 
-    p.section = seImplementation
-    result = newNodeP(nkCommentStmt, p)
-    result.comment = "# implementation"
-    getTok(p)
-  of pxInterface: 
-    p.section = seInterface
-    getTok(p)
-  of pxComment: 
-    result = newNodeP(nkCommentStmt, p)
-    skipCom(p, result)
-  of pxSemicolon: getTok(p)
-  of pxSymbol: 
-    if p.tok.ident.id == getIdent("break").id: 
-      result = newNodeP(nkBreakStmt, p)
-      getTok(p)
-      skipCom(p, result)
-      addSon(result, ast.emptyNode)
-    elif p.tok.ident.id == getIdent("continue").id: 
-      result = newNodeP(nkContinueStmt, p)
-      getTok(p)
-      skipCom(p, result)
-      addSon(result, ast.emptyNode)
-    elif p.tok.ident.id == getIdent("exit").id: 
-      result = newNodeP(nkReturnStmt, p)
-      getTok(p)
-      skipCom(p, result)
-      addSon(result, ast.emptyNode)
-    else: 
-      result = parseExprStmt(p)
-  of pxDot: getTok(p) # BUGFIX for ``end.`` in main program
-  else: result = parseExprStmt(p)
-  opt(p, pxSemicolon)
-  if result.kind != nkEmpty: skipCom(p, result)
-  p.context = oldcontext
-
-proc parseUnit(p: var TParser): PNode = 
-  result = newNodeP(nkStmtList, p)
-  getTok(p)                   # read first token
-  while true: 
-    case p.tok.xkind
-    of pxEof, pxEnd: break 
-    of pxBegin: parseBegin(p, result)
-    of pxCurlyDirLe, pxStarDirLe: 
-      if isHandledDirective(p): addSon(result, parseDirective(p))
-      else: parMessage(p, errXNotAllowedHere, p.tok.ident.s)
-    else: addSon(result, parseStmt(p))
-  opt(p, pxEnd)
-  opt(p, pxDot)
-  if p.tok.xkind != pxEof: 
-    addSon(result, parseStmt(p)) # comments after final 'end.'
-  
48f481df14dbd'>814fcb263 ^
244c14db0 ^








814fcb263 ^
244c14db0 ^



814fcb263 ^
244c14db0 ^
814fcb263 ^
244c14db0 ^




814fcb263 ^
244c14db0 ^




2591ac0ad ^






244c14db0 ^



7d6500f1d ^
244c14db0 ^















da190876d ^
244c14db0 ^





da190876d ^
244c14db0 ^



















244c14db0 ^
814fcb263 ^





7a2c11d3c ^














814fcb263 ^
1fada12a5 ^
814fcb263 ^














244c14db0 ^
814fcb263 ^
244c14db0 ^
814fcb263 ^







ed28f3c8d ^
814fcb263 ^

















244c14db0 ^
814fcb263 ^









1fada12a5 ^



814fcb263 ^








7a2c11d3c ^




814fcb263 ^

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805