summary refs log tree commit diff stats
path: root/tests/stdlib
diff options
context:
space:
mode:
authorAraq <rumpf_a@web.de>2014-01-13 02:10:03 +0100
committerAraq <rumpf_a@web.de>2014-01-13 02:10:03 +0100
commit20b5f31c03fb556ec0aa2428a40adbac004d8987 (patch)
tree58086941e7d6bb8f480ca1173a95722ada9435b2 /tests/stdlib
parent51ee524109cf7e3e86c676bc1676063a01bfd979 (diff)
downloadNim-20b5f31c03fb556ec0aa2428a40adbac004d8987.tar.gz
new tester; all tests categorized
Diffstat (limited to 'tests/stdlib')
-rw-r--r--tests/stdlib/tcputime.nim13
-rw-r--r--tests/stdlib/tcritbits.nim28
-rw-r--r--tests/stdlib/tdialogs.nim17
-rw-r--r--tests/stdlib/techo.nim3
-rw-r--r--tests/stdlib/tformat.nim12
-rw-r--r--tests/stdlib/thashes.nim8
-rw-r--r--tests/stdlib/tio.nim7
-rw-r--r--tests/stdlib/tircbot.nim452
-rw-r--r--tests/stdlib/tlists.nim66
-rw-r--r--tests/stdlib/tmarshal.nim65
-rw-r--r--tests/stdlib/tmath.nim47
-rw-r--r--tests/stdlib/tmath2.nim85
-rw-r--r--tests/stdlib/tmongo.nim16
-rw-r--r--tests/stdlib/tos.nim12
-rw-r--r--tests/stdlib/tparscfg.nim25
-rw-r--r--tests/stdlib/tparsefloat.nim3
-rw-r--r--tests/stdlib/tparsopt.nim27
-rw-r--r--tests/stdlib/tpegs.nim1770
-rw-r--r--tests/stdlib/tposix.nim16
-rw-r--r--tests/stdlib/tquit.nim6
-rw-r--r--tests/stdlib/tregex.nim31
-rw-r--r--tests/stdlib/treguse.nim27
-rw-r--r--tests/stdlib/trepr.nim29
-rw-r--r--tests/stdlib/trepr2.nim32
-rw-r--r--tests/stdlib/tsockets.nim11
-rw-r--r--tests/stdlib/tsortcall.nim5
-rw-r--r--tests/stdlib/tsplit.nim20
-rw-r--r--tests/stdlib/tstreams.nim7
-rw-r--r--tests/stdlib/tstrset.nim74
-rw-r--r--tests/stdlib/tstrtabs.nim12
-rw-r--r--tests/stdlib/tstrutil.nim45
-rw-r--r--tests/stdlib/ttime.nim6
-rw-r--r--tests/stdlib/tunidecode.nim12
-rw-r--r--tests/stdlib/twalker.nim13
-rw-r--r--tests/stdlib/txmlgen.nim12
-rw-r--r--tests/stdlib/txmltree.nim13
36 files changed, 3027 insertions, 0 deletions
diff --git a/tests/stdlib/tcputime.nim b/tests/stdlib/tcputime.nim
new file mode 100644
index 000000000..2fc46ee64
--- /dev/null
+++ b/tests/stdlib/tcputime.nim
@@ -0,0 +1,13 @@
+
+import times, os
+
+var e = epochTime()
+var c = cpuTime()
+
+os.sleep(1500)
+
+e = epochTime() - e
+c = cpuTime() - c
+
+echo "epochTime: ", e, " cpuTime: ", c
+
diff --git a/tests/stdlib/tcritbits.nim b/tests/stdlib/tcritbits.nim
new file mode 100644
index 000000000..fb447b80b
--- /dev/null
+++ b/tests/stdlib/tcritbits.nim
@@ -0,0 +1,28 @@
+discard """
+  output: '''abc
+def
+definition
+prefix
+xyz
+def
+definition'''
+"""
+
+import critbits
+
+when isMainModule:
+  var r: TCritBitTree[void]
+  r.incl "abc"
+  r.incl "xyz"
+  r.incl "def"
+  r.incl "definition"
+  r.incl "prefix"
+  doAssert r.contains"def"
+  #r.del "def"
+
+  for w in r.items:
+    echo w
+    
+  for w in r.itemsWithPrefix("de"):
+    echo w
+
diff --git a/tests/stdlib/tdialogs.nim b/tests/stdlib/tdialogs.nim
new file mode 100644
index 000000000..d161a976d
--- /dev/null
+++ b/tests/stdlib/tdialogs.nim
@@ -0,0 +1,17 @@
+# Test the dialogs module
+
+import dialogs, gtk2
+
+gtk2.nimrod_init()
+
+var x = ChooseFilesToOpen(nil)
+for a in items(x):
+  writeln(stdout, a)
+
+info(nil, "start with an info box")
+warning(nil, "now a warning ...")
+error(nil, "... and an error!")
+
+writeln(stdout, ChooseFileToOpen(nil))
+writeln(stdout, ChooseFileToSave(nil))
+writeln(stdout, ChooseDir(nil))
diff --git a/tests/stdlib/techo.nim b/tests/stdlib/techo.nim
new file mode 100644
index 000000000..0fa4b5fe0
--- /dev/null
+++ b/tests/stdlib/techo.nim
@@ -0,0 +1,3 @@
+# Simplest Nimrod program
+
+echo "Hello, World!"
diff --git a/tests/stdlib/tformat.nim b/tests/stdlib/tformat.nim
new file mode 100644
index 000000000..92c0c16f5
--- /dev/null
+++ b/tests/stdlib/tformat.nim
@@ -0,0 +1,12 @@
+discard """
+  file: "tformat.nim"
+  output: "Hi Andreas! How do you feel, Rumpf?"
+"""
+# Tests the new format proc (including the & and &= operators)

+

+import strutils

+

+echo("Hi $1! How do you feel, $2?\n" % ["Andreas", "Rumpf"])

+#OUT Hi Andreas! How do you feel, Rumpf?

+
+
diff --git a/tests/stdlib/thashes.nim b/tests/stdlib/thashes.nim
new file mode 100644
index 000000000..c442b43fb
--- /dev/null
+++ b/tests/stdlib/thashes.nim
@@ -0,0 +1,8 @@
+import unittest
+import hashes
+
+suite "hashes":
+  suite "hashing":
+    test "0.0 and -0.0 should have the same hash value":
+      var dummy = 0.0
+      check hash(dummy) == hash(-dummy)
diff --git a/tests/stdlib/tio.nim b/tests/stdlib/tio.nim
new file mode 100644
index 000000000..5ae119f77
--- /dev/null
+++ b/tests/stdlib/tio.nim
@@ -0,0 +1,7 @@
+# test the file-IO

+

+proc main() =

+  for line in lines("thello.nim"):

+    writeln(stdout, line)

+

+main()

diff --git a/tests/stdlib/tircbot.nim b/tests/stdlib/tircbot.nim
new file mode 100644
index 000000000..6008838ff
--- /dev/null
+++ b/tests/stdlib/tircbot.nim
@@ -0,0 +1,452 @@
+import irc, sockets, asyncio, json, os, strutils, times, redis
+
+type
+  TDb* = object
+    r*: TRedis
+    lastPing: float
+
+  TBuildResult* = enum
+    bUnknown, bFail, bSuccess
+
+  TTestResult* = enum
+    tUnknown, tFail, tSuccess
+
+  TEntry* = tuple[c: TCommit, p: seq[TPlatform]]
+  
+  TCommit* = object
+    commitMsg*, username*, hash*: string
+    date*: TTime
+
+  TPlatform* = object
+    buildResult*: TBuildResult
+    testResult*: TTestResult
+    failReason*, platform*: string
+    total*, passed*, skipped*, failed*: biggestInt
+    csources*: bool
+
+const
+  listName = "commits"
+  failOnExisting = False
+
+proc open*(host = "localhost", port: TPort): TDb =
+  result.r = redis.open(host, port)
+  result.lastPing = epochTime()
+
+discard """proc customHSet(database: TDb, name, field, value: string) =
+  if database.r.hSet(name, field, value).int == 0:
+    if failOnExisting:
+      assert(false)
+    else:
+      echo("[Warning:REDIS] ", field, " already exists in ", name)"""
+
+proc updateProperty*(database: TDb, commitHash, platform, property,
+                    value: string) =
+  var name = platform & ":" & commitHash
+  if database.r.hSet(name, property, value).int == 0:
+    echo("[INFO:REDIS] '$1' field updated in hash" % [property])
+  else:
+    echo("[INFO:REDIS] '$1' new field added to hash" % [property])
+
+proc globalProperty*(database: TDb, commitHash, property, value: string) =
+  if database.r.hSet(commitHash, property, value).int == 0:
+    echo("[INFO:REDIS] '$1' field updated in hash" % [property])
+  else:
+    echo("[INFO:REDIS] '$1' new field added to hash" % [property])
+
+proc addCommit*(database: TDb, commitHash, commitMsg, user: string) =
+  # Add the commit hash to the `commits` list.
+  discard database.r.lPush(listName, commitHash)
+  # Add the commit message, current date and username as a property
+  globalProperty(database, commitHash, "commitMsg", commitMsg)
+  globalProperty(database, commitHash, "date", $int(getTime()))
+  globalProperty(database, commitHash, "username", user)
+
+proc keepAlive*(database: var TDb) =
+  ## Keep the connection alive. Ping redis in this case. This functions does
+  ## not guarantee that redis will be pinged.
+  var t = epochTime()
+  if t - database.lastPing >= 60.0:
+    echo("PING -> redis")
+    assert(database.r.ping() == "PONG")
+    database.lastPing = t
+    
+proc getCommits*(database: TDb,
+                 plStr: var seq[string]): seq[TEntry] =
+  result = @[]
+  var commitsRaw = database.r.lrange("commits", 0, -1)
+  for c in items(commitsRaw):
+    var commit: TCommit
+    commit.hash = c
+    for key, value in database.r.hPairs(c):
+      case normalize(key)
+      of "commitmsg": commit.commitMsg = value
+      of "date": commit.date = TTime(parseInt(value))
+      of "username": commit.username = value
+      else:
+        echo(key)
+        assert(false)
+
+    var platformsRaw = database.r.lrange(c & ":platforms", 0, -1)
+    var platforms: seq[TPlatform] = @[]
+    for p in items(platformsRaw):
+      var platform: TPlatform
+      for key, value in database.r.hPairs(p & ":" & c):
+        case normalize(key)
+        of "buildresult":
+          platform.buildResult = parseInt(value).TBuildResult
+        of "testresult":
+          platform.testResult = parseInt(value).TTestResult
+        of "failreason":
+          platform.failReason = value
+        of "total":
+          platform.total = parseBiggestInt(value)
+        of "passed":
+          platform.passed = parseBiggestInt(value)
+        of "skipped":
+          platform.skipped = parseBiggestInt(value)
+        of "failed":
+          platform.failed = parseBiggestInt(value)
+        of "csources":
+          platform.csources = if value == "t": true else: false
+        else:
+          echo(normalize(key))
+          assert(false)
+      
+      platform.platform = p
+      
+      platforms.add(platform)
+      if p notin plStr:
+        plStr.add(p)
+    result.add((commit, platforms))
+
+proc commitExists*(database: TDb, commit: string, starts = false): bool =
+  # TODO: Consider making the 'commits' list a set.
+  for c in items(database.r.lrange("commits", 0, -1)):
+    if starts:
+      if c.startsWith(commit): return true
+    else:
+      if c == commit: return true
+  return false
+
+proc platformExists*(database: TDb, commit: string, platform: string): bool =
+  for p in items(database.r.lrange(commit & ":" & "platforms", 0, -1)):
+    if p == platform: return true
+
+proc expandHash*(database: TDb, commit: string): string =
+  for c in items(database.r.lrange("commits", 0, -1)):
+    if c.startsWith(commit): return c
+  assert false
+
+proc isNewest*(database: TDb, commit: string): bool =
+  return database.r.lIndex("commits", 0) == commit
+
+proc getNewest*(database: TDb): string =
+  return database.r.lIndex("commits", 0)
+
+proc addPlatform*(database: TDb, commit: string, platform: string) =
+  assert database.commitExists(commit)
+  assert (not database.platformExists(commit, platform))
+  var name = platform & ":" & commit
+  if database.r.exists(name):
+    if failOnExisting: quit("[FAIL] " & name & " already exists!", 1)
+    else: echo("[Warning] " & name & " already exists!")
+
+  discard database.r.lPush(commit & ":" & "platforms", platform)
+
+proc `[]`*(p: seq[TPlatform], name: string): TPlatform =
+  for platform in items(p):
+    if platform.platform == name:
+      return platform
+  raise newException(EInvalidValue, name & " platforms not found in commits.")
+  
+proc contains*(p: seq[TPlatform], s: string): bool =
+  for i in items(p):
+    if i.platform == s:
+      return True
+    
+
+type
+  PState = ref TState
+  TState = object of TObject
+    dispatcher: PDispatcher
+    sock: PAsyncSocket
+    ircClient: PAsyncIRC
+    hubPort: TPort
+    database: TDb
+    dbConnected: bool
+
+  TSeenType = enum
+    PSeenJoin, PSeenPart, PSeenMsg, PSeenNick, PSeenQuit
+  
+  TSeen = object
+    nick: string
+    channel: string
+    timestamp: TTime
+    case kind*: TSeenType
+    of PSeenJoin: nil
+    of PSeenPart, PSeenQuit, PSeenMsg:
+      msg: string
+    of PSeenNick:
+      newNick: string
+
+const
+  ircServer = "irc.freenode.net"
+  joinChans = @["#nimrod"]
+  botNickname = "NimBot"
+
+proc setSeen(d: TDb, s: TSeen) =
+  discard d.r.del("seen:" & s.nick)
+
+  var hashToSet = @[("type", $s.kind.int), ("channel", s.channel),
+                    ("timestamp", $s.timestamp.int)]
+  case s.kind
+  of PSeenJoin: nil
+  of PSeenPart, PSeenMsg, PSeenQuit:
+    hashToSet.add(("msg", s.msg))
+  of PSeenNick:
+    hashToSet.add(("newnick", s.newNick))
+  
+  d.r.hMSet("seen:" & s.nick, hashToSet)
+
+proc getSeen(d: TDb, nick: string, s: var TSeen): bool =
+  if d.r.exists("seen:" & nick):
+    result = true
+    s.nick = nick
+    # Get the type first
+    s.kind = d.r.hGet("seen:" & nick, "type").parseInt.TSeenType
+    
+    for key, value in d.r.hPairs("seen:" & nick):
+      case normalize(key)
+      of "type":
+        #s.kind = value.parseInt.TSeenType
+      of "channel":
+        s.channel = value
+      of "timestamp":
+        s.timestamp = TTime(value.parseInt)
+      of "msg":
+        s.msg = value
+      of "newnick":
+        s.newNick = value
+
+template createSeen(typ: TSeenType, n, c: string): stmt {.immediate, dirty.} =
+  var seenNick: TSeen
+  seenNick.kind = typ
+  seenNick.nick = n
+  seenNick.channel = c
+  seenNick.timestamp = getTime()
+
+proc parseReply(line: string, expect: string): Bool =
+  var jsonDoc = parseJson(line)
+  return jsonDoc["reply"].str == expect
+
+proc limitCommitMsg(m: string): string =
+  ## Limits the message to 300 chars and adds ellipsis.
+  var m1 = m
+  if NewLines in m1:
+    m1 = m1.splitLines()[0]
+  
+  if m1.len >= 300:
+    m1 = m1[0..300]
+
+  if m1.len >= 300 or NewLines in m: m1.add("... ")
+
+  if NewLines in m: m1.add($m.splitLines().len & " more lines")
+
+  return m1
+
+proc handleWebMessage(state: PState, line: string) =
+  echo("Got message from hub: " & line)
+  var json = parseJson(line)
+  if json.hasKey("payload"):
+    for i in 0..min(4, json["payload"]["commits"].len-1):
+      var commit = json["payload"]["commits"][i]
+      # Create the message
+      var message = ""
+      message.add(json["payload"]["repository"]["owner"]["name"].str & "/" &
+                  json["payload"]["repository"]["name"].str & " ")
+      message.add(commit["id"].str[0..6] & " ")
+      message.add(commit["author"]["name"].str & " ")
+      message.add("[+" & $commit["added"].len & " ")
+      message.add("±" & $commit["modified"].len & " ")
+      message.add("-" & $commit["removed"].len & "]: ")
+      message.add(limitCommitMsg(commit["message"].str))
+
+      # Send message to #nimrod.
+      state.ircClient.privmsg(joinChans[0], message)
+  elif json.hasKey("redisinfo"):
+    assert json["redisinfo"].hasKey("port")
+    #let redisPort = json["redisinfo"]["port"].num
+    state.dbConnected = true
+
+proc hubConnect(state: PState)
+proc handleConnect(s: PAsyncSocket, state: PState) =
+  try:
+    # Send greeting
+    var obj = newJObject()
+    obj["name"] = newJString("irc")
+    obj["platform"] = newJString("?")
+    state.sock.send($obj & "\c\L")
+
+    # Wait for reply.
+    var line = ""
+    sleep(1500)
+    if state.sock.recvLine(line):
+      assert(line != "")
+      doAssert parseReply(line, "OK")
+      echo("The hub accepted me!")
+    else:
+      raise newException(EInvalidValue,
+                         "Hub didn't accept me. Waited 1.5 seconds.")
+    
+    # ask for the redis info
+    var riobj = newJObject()
+    riobj["do"] = newJString("redisinfo")
+    state.sock.send($riobj & "\c\L")
+    
+  except EOS:
+    echo(getCurrentExceptionMsg())
+    s.close()
+    echo("Waiting 5 seconds...")
+    sleep(5000)
+    state.hubConnect()
+
+proc handleRead(s: PAsyncSocket, state: PState) =
+  var line = ""
+  if state.sock.recvLine(line):
+    if line != "":
+      # Handle the message
+      state.handleWebMessage(line)
+    else:
+      echo("Disconnected from hub: ", OSErrorMsg())
+      s.close()
+      echo("Reconnecting...")
+      state.hubConnect()
+  else:
+    echo(OSErrorMsg())
+
+proc hubConnect(state: PState) =
+  state.sock = AsyncSocket()
+  state.sock.connect("127.0.0.1", state.hubPort)
+  state.sock.handleConnect =
+    proc (s: PAsyncSocket) =
+      handleConnect(s, state)
+  state.sock.handleRead =
+    proc (s: PAsyncSocket) =
+      handleRead(s, state)
+
+  state.dispatcher.register(state.sock)
+
+proc handleIrc(irc: PAsyncIRC, event: TIRCEvent, state: PState) =
+  case event.typ
+  of EvConnected: nil
+  of EvDisconnected:
+    while not state.ircClient.isConnected:
+      try:
+        state.ircClient.connect()
+      except:
+        echo("Error reconnecting: ", getCurrentExceptionMsg())
+      
+      echo("Waiting 5 seconds...")
+      sleep(5000)
+    echo("Reconnected successfully!")
+  of EvMsg:
+    echo("< ", event.raw)
+    case event.cmd
+    of MPrivMsg:
+      let msg = event.params[event.params.len-1]
+      let words = msg.split(' ')
+      template pm(msg: string): stmt =
+        state.ircClient.privmsg(event.origin, msg)
+      case words[0]
+      of "!ping": pm("pong")
+      of "!lag":
+        if state.ircClient.getLag != -1.0:
+          var lag = state.ircClient.getLag
+          lag = lag * 1000.0
+          pm($int(lag) & "ms between me and the server.")
+        else:
+          pm("Unknown.")
+      of "!seen":
+        if words.len > 1:
+          let nick = words[1]
+          if nick == botNickname:
+            pm("Yes, I see myself.")
+          echo(nick)
+          var seenInfo: TSeen
+          if state.database.getSeen(nick, seenInfo):
+            #var mSend = ""
+            case seenInfo.kind
+            of PSeenMsg:
+              pm("$1 was last seen on $2 in $3 saying: $4" %
+                    [seenInfo.nick, $seenInfo.timestamp,
+                     seenInfo.channel, seenInfo.msg])
+            of PSeenJoin:
+              pm("$1 was last seen on $2 joining $3" %
+                        [seenInfo.nick, $seenInfo.timestamp, seenInfo.channel])
+            of PSeenPart:
+              pm("$1 was last seen on $2 leaving $3 with message: $4" %
+                        [seenInfo.nick, $seenInfo.timestamp, seenInfo.channel,
+                         seenInfo.msg])
+            of PSeenQuit:
+              pm("$1 was last seen on $2 quitting with message: $3" %
+                        [seenInfo.nick, $seenInfo.timestamp, seenInfo.msg])
+            of PSeenNick:
+              pm("$1 was last seen on $2 changing nick to $3" %
+                        [seenInfo.nick, $seenInfo.timestamp, seenInfo.newNick])
+            
+          else:
+            pm("I have not seen " & nick)
+        else:
+          pm("Syntax: !seen <nick>")
+
+      # TODO: ... commands
+
+      # -- Seen
+      # Log this as activity.
+      createSeen(PSeenMsg, event.nick, event.origin)
+      seenNick.msg = msg
+      state.database.setSeen(seenNick)
+    of MJoin:
+      createSeen(PSeenJoin, event.nick, event.origin)
+      state.database.setSeen(seenNick)
+    of MPart:
+      createSeen(PSeenPart, event.nick, event.origin)
+      let msg = event.params[event.params.high]
+      seenNick.msg = msg
+      state.database.setSeen(seenNick)
+    of MQuit:
+      createSeen(PSeenQuit, event.nick, event.origin)
+      let msg = event.params[event.params.high]
+      seenNick.msg = msg
+      state.database.setSeen(seenNick)
+    of MNick:
+      createSeen(PSeenNick, event.nick, "#nimrod")
+      seenNick.newNick = event.params[0]
+      state.database.setSeen(seenNick)
+    else:
+      nil # TODO: ?
+
+proc open(port: TPort = TPort(5123)): PState =
+  var res: PState
+  new(res)
+  res.dispatcher = newDispatcher()
+  
+  res.hubPort = port
+  res.hubConnect()
+  let hirc =
+    proc (a: PAsyncIRC, ev: TIRCEvent) =
+      handleIrc(a, ev, res)
+  # Connect to the irc server.
+  res.ircClient = AsyncIrc(ircServer, nick = botNickname, user = botNickname,
+                 joinChans = joinChans, ircEvent = hirc)
+  res.ircClient.connect()
+  res.dispatcher.register(res.ircClient)
+
+  res.dbConnected = false
+  result = res
+
+var state = tircbot.open() # Connect to the website and the IRC server.
+
+while state.dispatcher.poll():
+  if state.dbConnected:
+    state.database.keepAlive()
diff --git a/tests/stdlib/tlists.nim b/tests/stdlib/tlists.nim
new file mode 100644
index 000000000..7d5379945
--- /dev/null
+++ b/tests/stdlib/tlists.nim
@@ -0,0 +1,66 @@
+discard """
+  output: '''true'''
+"""
+
+import lists
+
+const
+  data = [1, 2, 3, 4, 5, 6]
+
+block SinglyLinkedListTest1:
+  var L: TSinglyLinkedList[int]
+  for d in items(data): L.prepend(d)
+  assert($L == "[6, 5, 4, 3, 2, 1]")
+  
+  assert(4 in L)
+
+block SinglyLinkedListTest2:
+  var L: TSinglyLinkedList[string]
+  for d in items(data): L.prepend($d)
+  assert($L == "[6, 5, 4, 3, 2, 1]")
+  
+  assert("4" in L)
+
+
+block DoublyLinkedListTest1:
+  var L: TDoublyLinkedList[int]
+  for d in items(data): L.prepend(d)
+  for d in items(data): L.append(d)
+  L.remove(L.find(1))
+  assert($L == "[6, 5, 4, 3, 2, 1, 2, 3, 4, 5, 6]")
+  
+  assert(4 in L)
+
+block SinglyLinkedRingTest1:
+  var L: TSinglyLinkedRing[int]
+  L.prepend(4)
+  assert($L == "[4]")
+  L.prepend(4)
+
+  assert($L == "[4, 4]")
+  assert(4 in L)
+  
+
+block DoublyLinkedRingTest1:
+  var L: TDoublyLinkedRing[int]
+  L.prepend(4)
+  assert($L == "[4]")
+  L.prepend(4)
+
+  assert($L == "[4, 4]")
+  assert(4 in L)
+  
+  L.append(3)
+  L.append(5)
+  assert($L == "[4, 4, 3, 5]")
+
+  L.remove(L.find(3))
+  L.remove(L.find(5))
+  L.remove(L.find(4))
+  L.remove(L.find(4))
+  assert($L == "[]")
+  assert(4 notin L)
+  
+
+echo "true"
+
diff --git a/tests/stdlib/tmarshal.nim b/tests/stdlib/tmarshal.nim
new file mode 100644
index 000000000..5471d347a
--- /dev/null
+++ b/tests/stdlib/tmarshal.nim
@@ -0,0 +1,65 @@
+discard """
+  output: ""
+"""
+
+import marshal
+
+template testit(x: expr) = echo($$to[type(x)]($$x))
+
+var x: array[0..4, array[0..4, string]] = [

+  ["test", "1", "2", "3", "4"], ["test", "1", "2", "3", "4"], 

+  ["test", "1", "2", "3", "4"], ["test", "1", "2", "3", "4"], 

+  ["test", "1", "2", "3", "4"]]

+testit(x)

+var test2: tuple[name: string, s: int] = ("tuple test", 56)
+testit(test2)
+
+type
+  TE = enum
+    blah, blah2
+
+  TestObj = object
+    test, asd: int
+    case test2: TE
+    of blah:
+      help: string
+    else:
+      nil
+      
+  PNode = ref TNode
+  TNode = object
+    next, prev: PNode
+    data: string
+
+proc buildList(): PNode =
+  new(result)
+  new(result.next)
+  new(result.prev)
+  result.data = "middle"
+  result.next.data = "next"
+  result.prev.data = "prev"
+  result.next.next = result.prev
+  result.next.prev = result
+  result.prev.next = result
+  result.prev.prev = result.next
+
+var test3: TestObj
+test3.test = 42
+test3.test2 = blah
+testit(test3)
+
+var test4: ref tuple[a, b: string]
+new(test4)
+test4.a = "ref string test: A"
+test4.b = "ref string test: B"
+testit(test4)
+
+var test5 = @[(0,1),(2,3),(4,5)]
+testit(test5)
+
+var test7 = buildList()
+testit(test7)
+
+var test6: set[char] = {'A'..'Z', '_'}
+testit(test6)
+
diff --git a/tests/stdlib/tmath.nim b/tests/stdlib/tmath.nim
new file mode 100644
index 000000000..a86a3b84c
--- /dev/null
+++ b/tests/stdlib/tmath.nim
@@ -0,0 +1,47 @@
+import math
+import unittest
+import sets
+
+suite "random int":
+  test "there might be some randomness":
+    var set = initSet[int](128)
+    randomize()
+    for i in 1..1000:
+      incl(set, random(high(int)))
+    check len(set) == 1000
+  test "single number bounds work":
+    randomize()
+    var rand: int
+    for i in 1..1000:
+      rand = random(1000)
+      check rand < 1000
+      check rand > -1
+  test "slice bounds work":
+    randomize()
+    var rand: int
+    for i in 1..1000:
+      rand = random(100..1000)
+      check rand < 1000
+      check rand >= 100
+
+suite "random float":
+  test "there might be some randomness":
+    var set = initSet[float](128)
+    randomize()
+    for i in 1..100:
+      incl(set, random(1.0))
+    check len(set) == 100
+  test "single number bounds work":
+    randomize()
+    var rand: float
+    for i in 1..1000:
+      rand = random(1000.0)
+      check rand < 1000.0
+      check rand > -1.0
+  test "slice bounds work":
+    randomize()
+    var rand: float
+    for i in 1..1000:
+      rand = random(100.0..1000.0)
+      check rand < 1000.0
+      check rand >= 100.0
diff --git a/tests/stdlib/tmath2.nim b/tests/stdlib/tmath2.nim
new file mode 100644
index 000000000..6a1dae54d
--- /dev/null
+++ b/tests/stdlib/tmath2.nim
@@ -0,0 +1,85 @@
+# tests for the interpreter

+

+proc loops(a: var int) =

+  nil

+  #var

+  #  b: int

+  #b = glob

+  #while b != 0:

+  #  b = b + 1

+  #a = b

+

+proc mymax(a, b: int): int =

+  #loops(result)

+  result = a

+  if b > a: result = b

+

+proc test(a, b: int) =

+  var

+    x, y: int

+  x = 0

+  y = 7

+  if x == a + b * 3 - 7 or

+      x == 8 or

+      x == y and y > -56 and y < 699:

+    y = 0

+  elif y == 78 and x == 0:

+    y = 1

+  elif y == 0 and x == 0:

+    y = 2

+  else:

+    y = 3

+

+type

+  TTokType = enum

+    tkNil, tkType, tkConst, tkVar, tkSymbol, tkIf,

+    tkWhile, tkFor, tkLoop, tkCase, tkLabel, tkGoto

+

+proc testCase(t: TTokType): int =

+  case t

+  of tkNil, tkType, tkConst: result = 0

+  of tkVar: result = 1

+  of tkSymbol: result = 2

+  of tkIf..tkFor: result = 3

+  of tkLoop: result = 56

+  else: result = -1

+  test(0, 9) # test the call

+

+proc TestLoops() =

+  var

+    i, j: int

+

+  while i >= 0:

+    if i mod 3 == 0:

+      break

+    i = i + 1

+    while j == 13:

+      j = 13

+      break

+    break

+

+  while True:

+    break

+

+

+var

+  glob: int

+  a: array [0..5, int]

+

+proc main() =

+  #glob = 0

+  #loops( glob )

+  var

+    res: int

+    s: string

+  #write(stdout, mymax(23, 45))

+  write(stdout, "Hallo! Wie heißt du? ")

+  s = readLine(stdin)

+  # test the case statement

+  case s

+  of "Andreas": write(stdout, "Du bist mein Meister!\n")

+  of "Rumpf": write(stdout, "Du bist in der Familie meines Meisters!\n")

+  else: write(stdout, "ich kenne dich nicht!\n")

+  write(stdout, "Du heisst " & s & "\n")

+

+main()

diff --git a/tests/stdlib/tmongo.nim b/tests/stdlib/tmongo.nim
new file mode 100644
index 000000000..1c4c0f4e6
--- /dev/null
+++ b/tests/stdlib/tmongo.nim
@@ -0,0 +1,16 @@
+
+import mongo, db_mongo, oids, json
+
+var conn = db_mongo.open()
+
+var data = %{"a": %13, "b": %"my string value", 
+             "inner": %{"i": %71} }
+
+var id = insertID(conn, "test.test", data)
+
+for v in find(conn, "test.test", "this.a == 13"):
+  print v
+
+delete(conn, "test.test", id)
+
+close(conn)
diff --git a/tests/stdlib/tos.nim b/tests/stdlib/tos.nim
new file mode 100644
index 000000000..fa9993cc9
--- /dev/null
+++ b/tests/stdlib/tos.nim
@@ -0,0 +1,12 @@
+# test some things of the os module
+
+import os
+
+proc walkDirTree(root: string) = 
+  for k, f in walkDir(root):
+    case k 
+    of pcFile, pcLinkToFile: echo(f)
+    of pcDir: walkDirTree(f)
+    of pcLinkToDir: nil
+
+walkDirTree(".")
diff --git a/tests/stdlib/tparscfg.nim b/tests/stdlib/tparscfg.nim
new file mode 100644
index 000000000..618ecadd6
--- /dev/null
+++ b/tests/stdlib/tparscfg.nim
@@ -0,0 +1,25 @@
+
+import
+  os, parsecfg, strutils, streams
+  
+var f = newFileStream(paramStr(1), fmRead)
+if f != nil:
+  var p: TCfgParser
+  open(p, f, paramStr(1))
+  while true:
+    var e = next(p)
+    case e.kind
+    of cfgEof: 
+      echo("EOF!")
+      break
+    of cfgSectionStart:   ## a ``[section]`` has been parsed
+      echo("new section: " & e.section)
+    of cfgKeyValuePair:
+      echo("key-value-pair: " & e.key & ": " & e.value)
+    of cfgOption:
+      echo("command: " & e.key & ": " & e.value)
+    of cfgError:
+      echo(e.msg)
+  close(p)
+else:
+  echo("cannot open: " & paramStr(1))
diff --git a/tests/stdlib/tparsefloat.nim b/tests/stdlib/tparsefloat.nim
new file mode 100644
index 000000000..38ed2db6d
--- /dev/null
+++ b/tests/stdlib/tparsefloat.nim
@@ -0,0 +1,3 @@
+import strutils
+
+echo ParseFloat("5000") / ParseFloat("10")
diff --git a/tests/stdlib/tparsopt.nim b/tests/stdlib/tparsopt.nim
new file mode 100644
index 000000000..2b2da7e51
--- /dev/null
+++ b/tests/stdlib/tparsopt.nim
@@ -0,0 +1,27 @@
+# Test the new parseopt module
+
+import
+  parseopt
+
+proc writeHelp() = 
+  writeln(stdout, "Usage: tparsopt [options] filename [options]")
+
+proc writeVersion() = 
+  writeln(stdout, "Version: 1.0.0")
+  
+var
+  filename = ""
+for kind, key, val in getopt():
+  case kind
+  of cmdArgument: 
+    filename = key
+  of cmdLongOption, cmdShortOption:
+    case key
+    of "help", "h": writeHelp()
+    of "version", "v": writeVersion()
+    else: 
+      writeln(stdout, "Unknown command line option: ", key, ": ", val)
+  of cmdEnd: assert(false) # cannot happen
+if filename == "":
+  # no filename has been given, so we show the help:
+  writeHelp()
diff --git a/tests/stdlib/tpegs.nim b/tests/stdlib/tpegs.nim
new file mode 100644
index 000000000..bdd8db0f8
--- /dev/null
+++ b/tests/stdlib/tpegs.nim
@@ -0,0 +1,1770 @@
+discard """
+  output: '''this
+is
+an
+example
+d
+e
+f
+('keyvalue' 'key'*)'''
+"""
+# PEGs module turned out to be a good test to detect memory management bugs.
+
+include "system/inclrtl"
+
+const
+  useUnicode = true ## change this to deactivate proper UTF-8 support
+
+import
+  strutils
+
+when useUnicode:
+  import unicode
+
+const
+  InlineThreshold = 5  ## number of leaves; -1 to disable inlining
+  MaxSubpatterns* = 10 ## defines the maximum number of subpatterns that
+                       ## can be captured. More subpatterns cannot be captured! 
+
+type
+  TPegKind = enum
+    pkEmpty,
+    pkAny,              ## any character (.)
+    pkAnyRune,          ## any Unicode character (_)
+    pkNewLine,          ## CR-LF, LF, CR
+    pkLetter,           ## Unicode letter
+    pkLower,            ## Unicode lower case letter
+    pkUpper,            ## Unicode upper case letter
+    pkTitle,            ## Unicode title character
+    pkWhitespace,       ## Unicode whitespace character
+    pkTerminal,
+    pkTerminalIgnoreCase,
+    pkTerminalIgnoreStyle,
+    pkChar,             ## single character to match
+    pkCharChoice,
+    pkNonTerminal,
+    pkSequence,         ## a b c ... --> Internal DSL: peg(a, b, c)
+    pkOrderedChoice,    ## a / b / ... --> Internal DSL: a / b or /[a, b, c]
+    pkGreedyRep,        ## a*     --> Internal DSL: *a
+                        ## a+     --> (a a*)
+    pkGreedyRepChar,    ## x* where x is a single character (superop)
+    pkGreedyRepSet,     ## [set]* (superop)
+    pkGreedyAny,        ## .* or _* (superop)
+    pkOption,           ## a?     --> Internal DSL: ?a
+    pkAndPredicate,     ## &a     --> Internal DSL: &a
+    pkNotPredicate,     ## !a     --> Internal DSL: !a
+    pkCapture,          ## {a}    --> Internal DSL: capture(a)
+    pkBackRef,          ## $i     --> Internal DSL: backref(i)
+    pkBackRefIgnoreCase,
+    pkBackRefIgnoreStyle,
+    pkSearch,           ## @a     --> Internal DSL: !*a
+    pkCapturedSearch,   ## {@} a  --> Internal DSL: !*\a
+    pkRule,             ## a <- b
+    pkList,             ## a, b
+    pkStartAnchor       ## ^      --> Internal DSL: startAnchor()
+  TNonTerminalFlag = enum
+    ntDeclared, ntUsed
+  TNonTerminal {.final.} = object ## represents a non terminal symbol
+    name: string                  ## the name of the symbol
+    line: int                     ## line the symbol has been declared/used in
+    col: int                      ## column the symbol has been declared/used in
+    flags: set[TNonTerminalFlag]  ## the nonterminal's flags
+    rule: TNode                   ## the rule that the symbol refers to
+  TNode {.final, shallow.} = object
+    case kind: TPegKind
+    of pkEmpty..pkWhitespace: nil
+    of pkTerminal, pkTerminalIgnoreCase, pkTerminalIgnoreStyle: term: string
+    of pkChar, pkGreedyRepChar: ch: char
+    of pkCharChoice, pkGreedyRepSet: charChoice: ref set[char]
+    of pkNonTerminal: nt: PNonTerminal
+    of pkBackRef..pkBackRefIgnoreStyle: index: range[0..MaxSubpatterns]
+    else: sons: seq[TNode]
+  PNonTerminal* = ref TNonTerminal
+  
+  TPeg* = TNode ## type that represents a PEG
+
+proc term*(t: string): TPeg {.rtl, extern: "npegs$1Str".} =
+  ## constructs a PEG from a terminal string
+  if t.len != 1:  
+    result.kind = pkTerminal
+    result.term = t
+  else:
+    result.kind = pkChar
+    result.ch = t[0]
+
+proc termIgnoreCase*(t: string): TPeg {.
+  rtl, extern: "npegs$1".} =
+  ## constructs a PEG from a terminal string; ignore case for matching
+  result.kind = pkTerminalIgnoreCase
+  result.term = t
+
+proc termIgnoreStyle*(t: string): TPeg {.
+  rtl, extern: "npegs$1".} =
+  ## constructs a PEG from a terminal string; ignore style for matching
+  result.kind = pkTerminalIgnoreStyle
+  result.term = t
+
+proc term*(t: char): TPeg {.rtl, extern: "npegs$1Char".} =
+  ## constructs a PEG from a terminal char
+  assert t != '\0'
+  result.kind = pkChar
+  result.ch = t
+  
+proc charSet*(s: set[char]): TPeg {.rtl, extern: "npegs$1".} =
+  ## constructs a PEG from a character set `s`
+  assert '\0' notin s
+  result.kind = pkCharChoice
+  new(result.charChoice)
+  result.charChoice[] = s
+
+proc len(a: TPeg): int {.inline.} = return a.sons.len
+proc add(d: var TPeg, s: TPeg) {.inline.} = add(d.sons, s)
+
+proc copyPeg(a: TPeg): TPeg = 
+  result.kind = a.kind
+  case a.kind
+  of pkEmpty..pkWhitespace: nil
+  of pkTerminal, pkTerminalIgnoreCase, pkTerminalIgnoreStyle: 
+    result.term = a.term
+  of pkChar, pkGreedyRepChar: 
+    result.ch = a.ch
+  of pkCharChoice, pkGreedyRepSet: 
+    new(result.charChoice)
+    result.charChoice[] = a.charChoice[]
+  of pkNonTerminal: result.nt = a.nt
+  of pkBackRef..pkBackRefIgnoreStyle: 
+    result.index = a.index
+  else: 
+    result.sons = a.sons
+
+proc addChoice(dest: var TPeg, elem: TPeg) =
+  var L = dest.len-1
+  if L >= 0 and dest.sons[L].kind == pkCharChoice: 
+    # caution! Do not introduce false aliasing here!
+    case elem.kind
+    of pkCharChoice:
+      dest.sons[L] = charSet(dest.sons[L].charChoice[] + elem.charChoice[])
+    of pkChar: 
+      dest.sons[L] = charSet(dest.sons[L].charChoice[] + {elem.ch})
+    else: add(dest, elem)
+  else: add(dest, elem)
+
+template multipleOp(k: TPegKind, localOpt: expr) =
+  result.kind = k
+  result.sons = @[]
+  for x in items(a):
+    if x.kind == k:
+      for y in items(x.sons):
+        localOpt(result, y)
+    else:
+      localOpt(result, x)
+  if result.len == 1:
+    result = result.sons[0]
+
+proc `/`*(a: varargs[TPeg]): TPeg {.
+  rtl, extern: "npegsOrderedChoice".} =
+  ## constructs an ordered choice with the PEGs in `a`
+  multipleOp(pkOrderedChoice, addChoice)
+
+proc addSequence(dest: var TPeg, elem: TPeg) =
+  var L = dest.len-1
+  if L >= 0 and dest.sons[L].kind == pkTerminal: 
+    # caution! Do not introduce false aliasing here!
+    case elem.kind
+    of pkTerminal: 
+      dest.sons[L] = term(dest.sons[L].term & elem.term)
+    of pkChar: 
+      dest.sons[L] = term(dest.sons[L].term & elem.ch)
+    else: add(dest, elem)
+  else: add(dest, elem)
+
+proc sequence*(a: varargs[TPeg]): TPeg {.
+  rtl, extern: "npegs$1".} =
+  ## constructs a sequence with all the PEGs from `a`
+  multipleOp(pkSequence, addSequence)
+ 
+proc `?`*(a: TPeg): TPeg {.rtl, extern: "npegsOptional".} =
+  ## constructs an optional for the PEG `a`
+  if a.kind in {pkOption, pkGreedyRep, pkGreedyAny, pkGreedyRepChar,
+                pkGreedyRepSet}:
+    # a* ?  --> a*
+    # a? ?  --> a?
+    result = a
+  else:
+    result.kind = pkOption
+    result.sons = @[a]
+
+proc `*`*(a: TPeg): TPeg {.rtl, extern: "npegsGreedyRep".} =
+  ## constructs a "greedy repetition" for the PEG `a`
+  case a.kind
+  of pkGreedyRep, pkGreedyRepChar, pkGreedyRepSet, pkGreedyAny, pkOption:
+    assert false
+    # produces endless loop!
+  of pkChar:
+    result.kind = pkGreedyRepChar
+    result.ch = a.ch
+  of pkCharChoice:
+    result.kind = pkGreedyRepSet
+    result.charChoice = a.charChoice # copying a reference suffices!
+  of pkAny, pkAnyRune:
+    result.kind = pkGreedyAny
+  else:
+    result.kind = pkGreedyRep
+    result.sons = @[a]
+
+proc `!*`*(a: TPeg): TPeg {.rtl, extern: "npegsSearch".} =
+  ## constructs a "search" for the PEG `a`
+  result.kind = pkSearch
+  result.sons = @[a]
+
+proc `!*\`*(a: TPeg): TPeg {.rtl, 
+                             extern: "npgegsCapturedSearch".} =
+  ## constructs a "captured search" for the PEG `a`
+  result.kind = pkCapturedSearch
+  result.sons = @[a]
+  
+when false:
+  proc contains(a: TPeg, k: TPegKind): bool =
+    if a.kind == k: return true
+    case a.kind
+    of pkEmpty, pkAny, pkAnyRune, pkGreedyAny, pkNewLine, pkTerminal,
+       pkTerminalIgnoreCase, pkTerminalIgnoreStyle, pkChar, pkGreedyRepChar,
+       pkCharChoice, pkGreedyRepSet: nil
+    of pkNonTerminal: return true
+    else:
+      for i in 0..a.sons.len-1:
+        if contains(a.sons[i], k): return true
+
+proc `+`*(a: TPeg): TPeg {.rtl, extern: "npegsGreedyPosRep".} =
+  ## constructs a "greedy positive repetition" with the PEG `a`
+  return sequence(a, *a)
+  
+proc `&`*(a: TPeg): TPeg {.rtl, extern: "npegsAndPredicate".} =
+  ## constructs an "and predicate" with the PEG `a`
+  result.kind = pkAndPredicate
+  result.sons = @[a]
+
+proc `!`*(a: TPeg): TPeg {.rtl, extern: "npegsNotPredicate".} =
+  ## constructs a "not predicate" with the PEG `a`
+  result.kind = pkNotPredicate
+  result.sons = @[a]
+
+proc any*: TPeg {.inline.} =
+  ## constructs the PEG `any character`:idx: (``.``)
+  result.kind = pkAny
+
+proc anyRune*: TPeg {.inline.} =
+  ## constructs the PEG `any rune`:idx: (``_``)
+  result.kind = pkAnyRune
+
+proc newLine*: TPeg {.inline.} =
+  ## constructs the PEG `newline`:idx: (``\n``)
+  result.kind = pkNewline
+
+proc UnicodeLetter*: TPeg {.inline.} = 
+  ## constructs the PEG ``\letter`` which matches any Unicode letter.
+  result.kind = pkLetter
+  
+proc UnicodeLower*: TPeg {.inline.} = 
+  ## constructs the PEG ``\lower`` which matches any Unicode lowercase letter.
+  result.kind = pkLower 
+
+proc UnicodeUpper*: TPeg {.inline.} = 
+  ## constructs the PEG ``\upper`` which matches any Unicode lowercase letter.
+  result.kind = pkUpper 
+  
+proc UnicodeTitle*: TPeg {.inline.} = 
+  ## constructs the PEG ``\title`` which matches any Unicode title letter.
+  result.kind = pkTitle
+
+proc UnicodeWhitespace*: TPeg {.inline.} = 
+  ## constructs the PEG ``\white`` which matches any Unicode 
+  ## whitespace character.
+  result.kind = pkWhitespace
+
+proc startAnchor*: TPeg {.inline.} = 
+  ## constructs the PEG ``^`` which matches the start of the input.  
+  result.kind = pkStartAnchor
+
+proc endAnchor*: TPeg {.inline.} = 
+  ## constructs the PEG ``$`` which matches the end of the input.  
+  result = !any()
+
+proc capture*(a: TPeg): TPeg {.rtl, extern: "npegsCapture".} =
+  ## constructs a capture with the PEG `a`
+  result.kind = pkCapture
+  result.sons = @[a]
+
+proc backref*(index: range[1..MaxSubPatterns]): TPeg {.
+  rtl, extern: "npegs$1".} = 
+  ## constructs a back reference of the given `index`. `index` starts counting
+  ## from 1.
+  result.kind = pkBackRef
+  result.index = index-1
+
+proc backrefIgnoreCase*(index: range[1..MaxSubPatterns]): TPeg {.
+  rtl, extern: "npegs$1".} = 
+  ## constructs a back reference of the given `index`. `index` starts counting
+  ## from 1. Ignores case for matching.
+  result.kind = pkBackRefIgnoreCase
+  result.index = index-1
+
+proc backrefIgnoreStyle*(index: range[1..MaxSubPatterns]): TPeg {.
+  rtl, extern: "npegs$1".}= 
+  ## constructs a back reference of the given `index`. `index` starts counting
+  ## from 1. Ignores style for matching.
+  result.kind = pkBackRefIgnoreStyle
+  result.index = index-1
+
+proc spaceCost(n: TPeg): int =
+  case n.kind
+  of pkEmpty: nil
+  of pkTerminal, pkTerminalIgnoreCase, pkTerminalIgnoreStyle, pkChar,
+     pkGreedyRepChar, pkCharChoice, pkGreedyRepSet, 
+     pkAny..pkWhitespace, pkGreedyAny:
+    result = 1
+  of pkNonTerminal:
+    # we cannot inline a rule with a non-terminal
+    result = InlineThreshold+1
+  else:
+    for i in 0..n.len-1:
+      inc(result, spaceCost(n.sons[i]))
+      if result >= InlineThreshold: break
+
+proc nonterminal*(n: PNonTerminal): TPeg {.
+  rtl, extern: "npegs$1".} = 
+  ## constructs a PEG that consists of the nonterminal symbol
+  assert n != nil
+  if ntDeclared in n.flags and spaceCost(n.rule) < InlineThreshold:
+    when false: echo "inlining symbol: ", n.name
+    result = n.rule # inlining of rule enables better optimizations
+  else:
+    result.kind = pkNonTerminal
+    result.nt = n
+
+proc newNonTerminal*(name: string, line, column: int): PNonTerminal {.
+  rtl, extern: "npegs$1".} =
+  ## constructs a nonterminal symbol
+  new(result)
+  result.name = name
+  result.line = line
+  result.col = column
+
+template letters*: expr =
+  ## expands to ``charset({'A'..'Z', 'a'..'z'})``
+  charset({'A'..'Z', 'a'..'z'})
+  
+template digits*: expr =
+  ## expands to ``charset({'0'..'9'})``
+  charset({'0'..'9'})
+
+template whitespace*: expr =
+  ## expands to ``charset({' ', '\9'..'\13'})``
+  charset({' ', '\9'..'\13'})
+  
+template identChars*: expr =
+  ## expands to ``charset({'a'..'z', 'A'..'Z', '0'..'9', '_'})``
+  charset({'a'..'z', 'A'..'Z', '0'..'9', '_'})
+  
+template identStartChars*: expr =
+  ## expands to ``charset({'A'..'Z', 'a'..'z', '_'})``
+  charset({'a'..'z', 'A'..'Z', '_'})
+
+template ident*: expr =
+  ## same as ``[a-zA-Z_][a-zA-z_0-9]*``; standard identifier
+  sequence(charset({'a'..'z', 'A'..'Z', '_'}),
+           *charset({'a'..'z', 'A'..'Z', '0'..'9', '_'}))
+  
+template natural*: expr =
+  ## same as ``\d+``
+  +digits
+
+# ------------------------- debugging -----------------------------------------
+
+proc esc(c: char, reserved = {'\0'..'\255'}): string = 
+  case c
+  of '\b': result = "\\b"
+  of '\t': result = "\\t"
+  of '\c': result = "\\c"
+  of '\L': result = "\\l"
+  of '\v': result = "\\v"
+  of '\f': result = "\\f"
+  of '\e': result = "\\e"
+  of '\a': result = "\\a"
+  of '\\': result = "\\\\"
+  of 'a'..'z', 'A'..'Z', '0'..'9', '_': result = $c
+  elif c < ' ' or c >= '\128': result = '\\' & $ord(c)
+  elif c in reserved: result = '\\' & c
+  else: result = $c
+  
+proc singleQuoteEsc(c: Char): string = return "'" & esc(c, {'\''}) & "'"
+
+proc singleQuoteEsc(str: string): string = 
+  result = "'"
+  for c in items(str): add result, esc(c, {'\''})
+  add result, '\''
+  
+proc charSetEscAux(cc: set[char]): string = 
+  const reserved = {'^', '-', ']'}
+  result = ""
+  var c1 = 0
+  while c1 <= 0xff: 
+    if chr(c1) in cc: 
+      var c2 = c1
+      while c2 < 0xff and chr(succ(c2)) in cc: inc(c2)
+      if c1 == c2: 
+        add result, esc(chr(c1), reserved)
+      elif c2 == succ(c1): 
+        add result, esc(chr(c1), reserved) & esc(chr(c2), reserved)
+      else: 
+        add result, esc(chr(c1), reserved) & '-' & esc(chr(c2), reserved)
+      c1 = c2
+    inc(c1)
+  
+proc CharSetEsc(cc: set[char]): string =
+  if card(cc) >= 128+64: 
+    result = "[^" & CharSetEscAux({'\1'..'\xFF'} - cc) & ']'
+  else: 
+    result = '[' & CharSetEscAux(cc) & ']'
+  
+proc toStrAux(r: TPeg, res: var string) = 
+  case r.kind
+  of pkEmpty: add(res, "()")
+  of pkAny: add(res, '.')
+  of pkAnyRune: add(res, '_')
+  of pkLetter: add(res, "\\letter")
+  of pkLower: add(res, "\\lower")
+  of pkUpper: add(res, "\\upper")
+  of pkTitle: add(res, "\\title")
+  of pkWhitespace: add(res, "\\white")
+
+  of pkNewline: add(res, "\\n")
+  of pkTerminal: add(res, singleQuoteEsc(r.term))
+  of pkTerminalIgnoreCase:
+    add(res, 'i')
+    add(res, singleQuoteEsc(r.term))
+  of pkTerminalIgnoreStyle:
+    add(res, 'y')
+    add(res, singleQuoteEsc(r.term))
+  of pkChar: add(res, singleQuoteEsc(r.ch))
+  of pkCharChoice: add(res, charSetEsc(r.charChoice[]))
+  of pkNonTerminal: add(res, r.nt.name)
+  of pkSequence:
+    add(res, '(')
+    toStrAux(r.sons[0], res)
+    for i in 1 .. high(r.sons):
+      add(res, ' ')
+      toStrAux(r.sons[i], res)
+    add(res, ')')
+  of pkOrderedChoice:
+    add(res, '(')
+    toStrAux(r.sons[0], res)
+    for i in 1 .. high(r.sons):
+      add(res, " / ")
+      toStrAux(r.sons[i], res)
+    add(res, ')')
+  of pkGreedyRep:
+    toStrAux(r.sons[0], res)
+    add(res, '*')
+  of pkGreedyRepChar:
+    add(res, singleQuoteEsc(r.ch))
+    add(res, '*')
+  of pkGreedyRepSet:
+    add(res, charSetEsc(r.charChoice[]))
+    add(res, '*')
+  of pkGreedyAny:
+    add(res, ".*")
+  of pkOption:
+    toStrAux(r.sons[0], res)
+    add(res, '?')
+  of pkAndPredicate:
+    add(res, '&')
+    toStrAux(r.sons[0], res)
+  of pkNotPredicate:
+    add(res, '!')
+    toStrAux(r.sons[0], res)
+  of pkSearch:
+    add(res, '@')
+    toStrAux(r.sons[0], res)
+  of pkCapturedSearch:
+    add(res, "{@}")
+    toStrAux(r.sons[0], res)
+  of pkCapture:
+    add(res, '{')
+    toStrAux(r.sons[0], res)    
+    add(res, '}')
+  of pkBackRef: 
+    add(res, '$')
+    add(res, $r.index)
+  of pkBackRefIgnoreCase: 
+    add(res, "i$")
+    add(res, $r.index)
+  of pkBackRefIgnoreStyle: 
+    add(res, "y$")
+    add(res, $r.index)
+  of pkRule:
+    toStrAux(r.sons[0], res)    
+    add(res, " <- ")
+    toStrAux(r.sons[1], res)
+  of pkList:
+    for i in 0 .. high(r.sons):
+      toStrAux(r.sons[i], res)
+      add(res, "\n")  
+  of pkStartAnchor:
+    add(res, '^')
+
+proc `$` *(r: TPeg): string {.rtl, extern: "npegsToString".} =
+  ## converts a PEG to its string representation
+  result = ""
+  toStrAux(r, result)
+
+# --------------------- core engine -------------------------------------------
+
+type
+  TCaptures* {.final.} = object ## contains the captured substrings.
+    matches: array[0..maxSubpatterns-1, tuple[first, last: int]]
+    ml: int
+    origStart: int
+
+proc bounds*(c: TCaptures, 
+             i: range[0..maxSubpatterns-1]): tuple[first, last: int] = 
+  ## returns the bounds ``[first..last]`` of the `i`'th capture.
+  result = c.matches[i]
+
+when not useUnicode:
+  type
+    TRune = char
+  template fastRuneAt(s, i, ch: expr) =
+    ch = s[i]
+    inc(i)
+  template runeLenAt(s, i: expr): expr = 1
+
+  proc isAlpha(a: char): bool {.inline.} = return a in {'a'..'z','A'..'Z'}
+  proc isUpper(a: char): bool {.inline.} = return a in {'A'..'Z'}
+  proc isLower(a: char): bool {.inline.} = return a in {'a'..'z'}
+  proc isTitle(a: char): bool {.inline.} = return false
+  proc isWhiteSpace(a: char): bool {.inline.} = return a in {' ', '\9'..'\13'}
+
+proc rawMatch*(s: string, p: TPeg, start: int, c: var TCaptures): int {.
+               rtl, extern: "npegs$1".} =
+  ## low-level matching proc that implements the PEG interpreter. Use this 
+  ## for maximum efficiency (every other PEG operation ends up calling this
+  ## proc).
+  ## Returns -1 if it does not match, else the length of the match
+  case p.kind
+  of pkEmpty: result = 0 # match of length 0
+  of pkAny:
+    if s[start] != '\0': result = 1
+    else: result = -1
+  of pkAnyRune:
+    if s[start] != '\0':
+      result = runeLenAt(s, start)
+    else:
+      result = -1
+  of pkLetter: 
+    if s[start] != '\0':
+      var a: TRune
+      result = start
+      fastRuneAt(s, result, a)
+      if isAlpha(a): dec(result, start)
+      else: result = -1
+    else:
+      result = -1
+  of pkLower: 
+    if s[start] != '\0':
+      var a: TRune
+      result = start
+      fastRuneAt(s, result, a)
+      if isLower(a): dec(result, start)
+      else: result = -1
+    else:
+      result = -1
+  of pkUpper: 
+    if s[start] != '\0':
+      var a: TRune
+      result = start
+      fastRuneAt(s, result, a)
+      if isUpper(a): dec(result, start)
+      else: result = -1
+    else:
+      result = -1
+  of pkTitle: 
+    if s[start] != '\0':
+      var a: TRune
+      result = start
+      fastRuneAt(s, result, a)
+      if isTitle(a): dec(result, start) 
+      else: result = -1
+    else:
+      result = -1
+  of pkWhitespace: 
+    if s[start] != '\0':
+      var a: TRune
+      result = start
+      fastRuneAt(s, result, a)
+      if isWhitespace(a): dec(result, start)
+      else: result = -1
+    else:
+      result = -1
+  of pkGreedyAny:
+    result = len(s) - start
+  of pkNewLine:
+    if s[start] == '\L': result = 1
+    elif s[start] == '\C':
+      if s[start+1] == '\L': result = 2
+      else: result = 1
+    else: result = -1
+  of pkTerminal:
+    result = len(p.term)
+    for i in 0..result-1:
+      if p.term[i] != s[start+i]:
+        result = -1
+        break
+  of pkTerminalIgnoreCase:
+    var
+      i = 0
+      a, b: TRune
+    result = start
+    while i < len(p.term):
+      fastRuneAt(p.term, i, a)
+      fastRuneAt(s, result, b)
+      if toLower(a) != toLower(b):
+        result = -1
+        break
+    dec(result, start)
+  of pkTerminalIgnoreStyle:
+    var
+      i = 0
+      a, b: TRune
+    result = start
+    while i < len(p.term):
+      while true:
+        fastRuneAt(p.term, i, a)
+        if a != TRune('_'): break
+      while true:
+        fastRuneAt(s, result, b)
+        if b != TRune('_'): break
+      if toLower(a) != toLower(b):
+        result = -1
+        break
+    dec(result, start)
+  of pkChar:
+    if p.ch == s[start]: result = 1
+    else: result = -1
+  of pkCharChoice:
+    if contains(p.charChoice[], s[start]): result = 1
+    else: result = -1
+  of pkNonTerminal:
+    var oldMl = c.ml
+    when false: echo "enter: ", p.nt.name
+    result = rawMatch(s, p.nt.rule, start, c)
+    when false: echo "leave: ", p.nt.name
+    if result < 0: c.ml = oldMl
+  of pkSequence:
+    var oldMl = c.ml  
+    result = 0
+    assert(not isNil(p.sons))
+    for i in 0..high(p.sons):
+      var x = rawMatch(s, p.sons[i], start+result, c)
+      if x < 0:
+        c.ml = oldMl
+        result = -1
+        break
+      else: inc(result, x)
+  of pkOrderedChoice:
+    var oldMl = c.ml
+    for i in 0..high(p.sons):
+      result = rawMatch(s, p.sons[i], start, c)
+      if result >= 0: break
+      c.ml = oldMl
+  of pkSearch:
+    var oldMl = c.ml
+    result = 0
+    while start+result < s.len:
+      var x = rawMatch(s, p.sons[0], start+result, c)
+      if x >= 0:
+        inc(result, x)
+        return
+      inc(result)
+    result = -1
+    c.ml = oldMl
+  of pkCapturedSearch:
+    var idx = c.ml # reserve a slot for the subpattern
+    inc(c.ml)
+    result = 0
+    while start+result < s.len:
+      var x = rawMatch(s, p.sons[0], start+result, c)
+      if x >= 0:
+        if idx < maxSubpatterns:
+          c.matches[idx] = (start, start+result-1)
+        #else: silently ignore the capture
+        inc(result, x)
+        return
+      inc(result)
+    result = -1
+    c.ml = idx
+  of pkGreedyRep:
+    result = 0
+    while true:
+      var x = rawMatch(s, p.sons[0], start+result, c)
+      # if x == 0, we have an endless loop; so the correct behaviour would be
+      # not to break. But endless loops can be easily introduced:
+      # ``(comment / \w*)*`` is such an example. Breaking for x == 0 does the
+      # expected thing in this case.
+      if x <= 0: break
+      inc(result, x)
+  of pkGreedyRepChar:
+    result = 0
+    var ch = p.ch
+    while ch == s[start+result]: inc(result)
+  of pkGreedyRepSet:
+    result = 0
+    while contains(p.charChoice[], s[start+result]): inc(result)
+  of pkOption:
+    result = max(0, rawMatch(s, p.sons[0], start, c))
+  of pkAndPredicate:
+    var oldMl = c.ml
+    result = rawMatch(s, p.sons[0], start, c)
+    if result >= 0: result = 0 # do not consume anything
+    else: c.ml = oldMl
+  of pkNotPredicate:
+    var oldMl = c.ml
+    result = rawMatch(s, p.sons[0], start, c)
+    if result < 0: result = 0
+    else:
+      c.ml = oldMl
+      result = -1
+  of pkCapture:
+    var idx = c.ml # reserve a slot for the subpattern
+    inc(c.ml)
+    result = rawMatch(s, p.sons[0], start, c)
+    if result >= 0:
+      if idx < maxSubpatterns:
+        c.matches[idx] = (start, start+result-1)
+      #else: silently ignore the capture
+    else:
+      c.ml = idx
+  of pkBackRef..pkBackRefIgnoreStyle: 
+    if p.index >= c.ml: return -1
+    var (a, b) = c.matches[p.index]
+    var n: TPeg
+    n.kind = succ(pkTerminal, ord(p.kind)-ord(pkBackRef)) 
+    n.term = s.substr(a, b)
+    result = rawMatch(s, n, start, c)
+  of pkStartAnchor:
+    if c.origStart == start: result = 0
+    else: result = -1
+  of pkRule, pkList: assert false
+
+proc match*(s: string, pattern: TPeg, matches: var openarray[string],
+            start = 0): bool {.rtl, extern: "npegs$1Capture".} =
+  ## returns ``true`` if ``s[start..]`` matches the ``pattern`` and
+  ## the captured substrings in the array ``matches``. If it does not
+  ## match, nothing is written into ``matches`` and ``false`` is
+  ## returned.
+  var c: TCaptures
+  c.origStart = start
+  result = rawMatch(s, pattern, start, c) == len(s) -start
+  if result:
+    for i in 0..c.ml-1:
+      matches[i] = substr(s, c.matches[i][0], c.matches[i][1])
+
+proc match*(s: string, pattern: TPeg, 
+            start = 0): bool {.rtl, extern: "npegs$1".} =
+  ## returns ``true`` if ``s`` matches the ``pattern`` beginning from ``start``.
+  var c: TCaptures
+  c.origStart = start
+  result = rawMatch(s, pattern, start, c) == len(s)-start
+
+proc matchLen*(s: string, pattern: TPeg, matches: var openarray[string],
+               start = 0): int {.rtl, extern: "npegs$1Capture".} =
+  ## the same as ``match``, but it returns the length of the match,
+  ## if there is no match, -1 is returned. Note that a match length
+  ## of zero can happen. It's possible that a suffix of `s` remains
+  ## that does not belong to the match.
+  var c: TCaptures
+  c.origStart = start
+  result = rawMatch(s, pattern, start, c)
+  if result >= 0:
+    for i in 0..c.ml-1:
+      matches[i] = substr(s, c.matches[i][0], c.matches[i][1])
+
+proc matchLen*(s: string, pattern: TPeg, 
+               start = 0): int {.rtl, extern: "npegs$1".} =
+  ## the same as ``match``, but it returns the length of the match,
+  ## if there is no match, -1 is returned. Note that a match length
+  ## of zero can happen. It's possible that a suffix of `s` remains
+  ## that does not belong to the match.
+  var c: TCaptures
+  c.origStart = start
+  result = rawMatch(s, pattern, start, c)
+
+proc find*(s: string, pattern: TPeg, matches: var openarray[string],
+           start = 0): int {.rtl, extern: "npegs$1Capture".} =
+  ## returns the starting position of ``pattern`` in ``s`` and the captured
+  ## substrings in the array ``matches``. If it does not match, nothing
+  ## is written into ``matches`` and -1 is returned.
+  for i in start .. s.len-1:
+    if matchLen(s, pattern, matches, i) >= 0: return i
+  return -1
+  # could also use the pattern here: (!P .)* P
+  
+proc findBounds*(s: string, pattern: TPeg, matches: var openarray[string],
+                 start = 0): tuple[first, last: int] {.
+                 rtl, extern: "npegs$1Capture".} =
+  ## returns the starting position and end position of ``pattern`` in ``s`` 
+  ## and the captured
+  ## substrings in the array ``matches``. If it does not match, nothing
+  ## is written into ``matches`` and (-1,0) is returned.
+  for i in start .. s.len-1:
+    var L = matchLen(s, pattern, matches, i)
+    if L >= 0: return (i, i+L-1)
+  return (-1, 0)
+  
+proc find*(s: string, pattern: TPeg, 
+           start = 0): int {.rtl, extern: "npegs$1".} =
+  ## returns the starting position of ``pattern`` in ``s``. If it does not
+  ## match, -1 is returned.
+  for i in start .. s.len-1:
+    if matchLen(s, pattern, i) >= 0: return i
+  return -1
+  
+iterator findAll*(s: string, pattern: TPeg, start = 0): string = 
+  ## yields all matching captures of pattern in `s`.
+  var matches: array[0..MaxSubpatterns-1, string]
+  var i = start
+  while i < s.len:
+    var L = matchLen(s, pattern, matches, i)
+    if L < 0: break
+    for k in 0..maxSubPatterns-1: 
+      if isNil(matches[k]): break
+      yield matches[k]
+    inc(i, L)
+    
+proc findAll*(s: string, pattern: TPeg, start = 0): seq[string] {.
+  rtl, extern: "npegs$1".} = 
+  ## returns all matching captures of pattern in `s`.
+  ## If it does not match, @[] is returned.
+  accumulateResult(findAll(s, pattern, start))
+  
+template `=~`*(s: string, pattern: TPeg): expr =
+  ## This calls ``match`` with an implicit declared ``matches`` array that 
+  ## can be used in the scope of the ``=~`` call: 
+  ## 
+  ## .. code-block:: nimrod
+  ##
+  ##   if line =~ peg"\s* {\w+} \s* '=' \s* {\w+}": 
+  ##     # matches a key=value pair:
+  ##     echo("Key: ", matches[0])
+  ##     echo("Value: ", matches[1])
+  ##   elif line =~ peg"\s*{'#'.*}":
+  ##     # matches a comment
+  ##     # note that the implicit ``matches`` array is different from the
+  ##     # ``matches`` array of the first branch
+  ##     echo("comment: ", matches[0])
+  ##   else:
+  ##     echo("syntax error")
+  ##  
+  when not definedInScope(matches):
+    var matches {.inject.}: array[0..maxSubpatterns-1, string]
+  match(s, pattern, matches)
+
+# ------------------------- more string handling ------------------------------
+
+proc contains*(s: string, pattern: TPeg, start = 0): bool {.
+  rtl, extern: "npegs$1".} =
+  ## same as ``find(s, pattern, start) >= 0``
+  return find(s, pattern, start) >= 0
+
+proc contains*(s: string, pattern: TPeg, matches: var openArray[string],
+              start = 0): bool {.rtl, extern: "npegs$1Capture".} =
+  ## same as ``find(s, pattern, matches, start) >= 0``
+  return find(s, pattern, matches, start) >= 0
+
+proc startsWith*(s: string, prefix: TPeg, start = 0): bool {.
+  rtl, extern: "npegs$1".} =
+  ## returns true if `s` starts with the pattern `prefix`
+  result = matchLen(s, prefix, start) >= 0
+
+proc endsWith*(s: string, suffix: TPeg, start = 0): bool {.
+  rtl, extern: "npegs$1".} =
+  ## returns true if `s` ends with the pattern `prefix`
+  for i in start .. s.len-1:
+    if matchLen(s, suffix, i) == s.len - i: return true
+
+proc replacef*(s: string, sub: TPeg, by: string): string {.
+  rtl, extern: "npegs$1".} =
+  ## Replaces `sub` in `s` by the string `by`. Captures can be accessed in `by`
+  ## with the notation ``$i`` and ``$#`` (see strutils.`%`). Examples:
+  ##
+  ## .. code-block:: nimrod
+  ##   "var1=key; var2=key2".replace(peg"{\ident}'='{\ident}", "$1<-$2$2")
+  ##
+  ## Results in:
+  ##
+  ## .. code-block:: nimrod
+  ##
+  ##   "var1<-keykey; val2<-key2key2"
+  result = ""
+  var i = 0
+  var caps: array[0..maxSubpatterns-1, string]
+  while i < s.len:
+    var x = matchLen(s, sub, caps, i)
+    if x <= 0:
+      add(result, s[i])
+      inc(i)
+    else:
+      addf(result, by, caps)
+      inc(i, x)
+  add(result, substr(s, i))
+
+proc replace*(s: string, sub: TPeg, by = ""): string {.
+  rtl, extern: "npegs$1".} =
+  ## Replaces `sub` in `s` by the string `by`. Captures cannot be accessed
+  ## in `by`.
+  result = ""
+  var i = 0
+  var caps: array[0..maxSubpatterns-1, string]
+  while i < s.len:
+    var x = matchLen(s, sub, caps, i)
+    if x <= 0:
+      add(result, s[i])
+      inc(i)
+    else:
+      addf(result, by, caps)
+      inc(i, x)
+  add(result, substr(s, i))
+  
+proc parallelReplace*(s: string, subs: varargs[
+                      tuple[pattern: TPeg, repl: string]]): string {.
+                      rtl, extern: "npegs$1".} = 
+  ## Returns a modified copy of `s` with the substitutions in `subs`
+  ## applied in parallel.
+  result = ""
+  var i = 0
+  var caps: array[0..maxSubpatterns-1, string]
+  while i < s.len:
+    block searchSubs:
+      for j in 0..high(subs):
+        var x = matchLen(s, subs[j][0], caps, i)
+        if x > 0:
+          addf(result, subs[j][1], caps)
+          inc(i, x)
+          break searchSubs
+      add(result, s[i])
+      inc(i)
+  # copy the rest:
+  add(result, substr(s, i))  
+  
+proc transformFile*(infile, outfile: string,
+                    subs: varargs[tuple[pattern: TPeg, repl: string]]) {.
+                    rtl, extern: "npegs$1".} =
+  ## reads in the file `infile`, performs a parallel replacement (calls
+  ## `parallelReplace`) and writes back to `outfile`. Calls ``quit`` if an
+  ## error occurs. This is supposed to be used for quick scripting.
+  var x = readFile(infile)
+  if not isNil(x):
+    var f: TFile
+    if open(f, outfile, fmWrite):
+      write(f, x.parallelReplace(subs))
+      close(f)
+    else:
+      quit("cannot open for writing: " & outfile)
+  else:
+    quit("cannot open for reading: " & infile)
+  
+iterator split*(s: string, sep: TPeg): string =
+  ## Splits the string `s` into substrings.
+  ##
+  ## Substrings are separated by the PEG `sep`.
+  ## Examples:
+  ##
+  ## .. code-block:: nimrod
+  ##   for word in split("00232this02939is39an22example111", peg"\d+"):
+  ##     writeln(stdout, word)
+  ##
+  ## Results in:
+  ##
+  ## .. code-block:: nimrod
+  ##   "this"
+  ##   "is"
+  ##   "an"
+  ##   "example"
+  ##
+  var
+    first = 0
+    last = 0
+  while last < len(s):
+    var x = matchLen(s, sep, last)
+    if x > 0: inc(last, x)
+    first = last
+    while last < len(s):
+      inc(last)
+      x = matchLen(s, sep, last)
+      if x > 0: break
+    if first < last:
+      yield substr(s, first, last-1)
+
+proc split*(s: string, sep: TPeg): seq[string] {.
+  rtl, extern: "npegs$1".} =
+  ## Splits the string `s` into substrings.
+  accumulateResult(split(s, sep))
+
+# ------------------- scanner -------------------------------------------------
+
+type
+  TModifier = enum
+    modNone,
+    modVerbatim,
+    modIgnoreCase,
+    modIgnoreStyle
+  TTokKind = enum       ## enumeration of all tokens
+    tkInvalid,          ## invalid token
+    tkEof,              ## end of file reached
+    tkAny,              ## .
+    tkAnyRune,          ## _
+    tkIdentifier,       ## abc
+    tkStringLit,        ## "abc" or 'abc'
+    tkCharSet,          ## [^A-Z]
+    tkParLe,            ## '('
+    tkParRi,            ## ')'
+    tkCurlyLe,          ## '{'
+    tkCurlyRi,          ## '}'
+    tkCurlyAt,          ## '{@}'
+    tkArrow,            ## '<-'
+    tkBar,              ## '/'
+    tkStar,             ## '*'
+    tkPlus,             ## '+'
+    tkAmp,              ## '&'
+    tkNot,              ## '!'
+    tkOption,           ## '?'
+    tkAt,               ## '@'
+    tkBuiltin,          ## \identifier
+    tkEscaped,          ## \\
+    tkBackref,          ## '$'
+    tkDollar,           ## '$'
+    tkHat               ## '^'
+  
+  TToken {.final.} = object  ## a token
+    kind: TTokKind           ## the type of the token
+    modifier: TModifier
+    literal: string          ## the parsed (string) literal
+    charset: set[char]       ## if kind == tkCharSet
+    index: int               ## if kind == tkBackref
+  
+  TPegLexer {.inheritable.} = object          ## the lexer object.
+    bufpos: int               ## the current position within the buffer
+    buf: cstring              ## the buffer itself
+    LineNumber: int           ## the current line number
+    lineStart: int            ## index of last line start in buffer
+    colOffset: int            ## column to add
+    filename: string
+
+const
+  tokKindToStr: array[TTokKind, string] = [
+    "invalid", "[EOF]", ".", "_", "identifier", "string literal",
+    "character set", "(", ")", "{", "}", "{@}",
+    "<-", "/", "*", "+", "&", "!", "?",
+    "@", "built-in", "escaped", "$", "$", "^"
+  ]
+
+proc HandleCR(L: var TPegLexer, pos: int): int =
+  assert(L.buf[pos] == '\c')
+  inc(L.linenumber)
+  result = pos+1
+  if L.buf[result] == '\L': inc(result)
+  L.lineStart = result
+
+proc HandleLF(L: var TPegLexer, pos: int): int =
+  assert(L.buf[pos] == '\L')
+  inc(L.linenumber)
+  result = pos+1
+  L.lineStart = result
+
+proc init(L: var TPegLexer, input, filename: string, line = 1, col = 0) = 
+  L.buf = input
+  L.bufpos = 0
+  L.lineNumber = line
+  L.colOffset = col
+  L.lineStart = 0
+  L.filename = filename
+
+proc getColumn(L: TPegLexer): int {.inline.} = 
+  result = abs(L.bufpos - L.lineStart) + L.colOffset
+
+proc getLine(L: TPegLexer): int {.inline.} = 
+  result = L.linenumber
+  
+proc errorStr(L: TPegLexer, msg: string, line = -1, col = -1): string =
+  var line = if line < 0: getLine(L) else: line
+  var col = if col < 0: getColumn(L) else: col
+  result = "$1($2, $3) Error: $4" % [L.filename, $line, $col, msg]
+
+proc handleHexChar(c: var TPegLexer, xi: var int) = 
+  case c.buf[c.bufpos]
+  of '0'..'9': 
+    xi = (xi shl 4) or (ord(c.buf[c.bufpos]) - ord('0'))
+    inc(c.bufpos)
+  of 'a'..'f': 
+    xi = (xi shl 4) or (ord(c.buf[c.bufpos]) - ord('a') + 10)
+    inc(c.bufpos)
+  of 'A'..'F': 
+    xi = (xi shl 4) or (ord(c.buf[c.bufpos]) - ord('A') + 10)
+    inc(c.bufpos)
+  else: nil
+
+proc getEscapedChar(c: var TPegLexer, tok: var TToken) = 
+  inc(c.bufpos)
+  case c.buf[c.bufpos]
+  of 'r', 'R', 'c', 'C': 
+    add(tok.literal, '\c')
+    Inc(c.bufpos)
+  of 'l', 'L': 
+    add(tok.literal, '\L')
+    Inc(c.bufpos)
+  of 'f', 'F': 
+    add(tok.literal, '\f')
+    inc(c.bufpos)
+  of 'e', 'E': 
+    add(tok.literal, '\e')
+    Inc(c.bufpos)
+  of 'a', 'A': 
+    add(tok.literal, '\a')
+    Inc(c.bufpos)
+  of 'b', 'B': 
+    add(tok.literal, '\b')
+    Inc(c.bufpos)
+  of 'v', 'V': 
+    add(tok.literal, '\v')
+    Inc(c.bufpos)
+  of 't', 'T': 
+    add(tok.literal, '\t')
+    Inc(c.bufpos)
+  of 'x', 'X': 
+    inc(c.bufpos)
+    var xi = 0
+    handleHexChar(c, xi)
+    handleHexChar(c, xi)
+    if xi == 0: tok.kind = tkInvalid
+    else: add(tok.literal, Chr(xi))
+  of '0'..'9': 
+    var val = ord(c.buf[c.bufpos]) - ord('0')
+    Inc(c.bufpos)
+    var i = 1
+    while (i <= 3) and (c.buf[c.bufpos] in {'0'..'9'}): 
+      val = val * 10 + ord(c.buf[c.bufpos]) - ord('0')
+      inc(c.bufpos)
+      inc(i)
+    if val > 0 and val <= 255: add(tok.literal, chr(val))
+    else: tok.kind = tkInvalid
+  of '\0'..'\31':
+    tok.kind = tkInvalid
+  elif c.buf[c.bufpos] in strutils.letters:
+    tok.kind = tkInvalid
+  else:
+    add(tok.literal, c.buf[c.bufpos])
+    Inc(c.bufpos)
+  
+proc skip(c: var TPegLexer) = 
+  var pos = c.bufpos
+  var buf = c.buf
+  while true: 
+    case buf[pos]
+    of ' ', '\t': 
+      Inc(pos)
+    of '#':
+      while not (buf[pos] in {'\c', '\L', '\0'}): inc(pos)
+    of '\c':
+      pos = HandleCR(c, pos)
+      buf = c.buf
+    of '\L': 
+      pos = HandleLF(c, pos)
+      buf = c.buf
+    else: 
+      break                   # EndOfFile also leaves the loop
+  c.bufpos = pos
+  
+proc getString(c: var TPegLexer, tok: var TToken) = 
+  tok.kind = tkStringLit
+  var pos = c.bufPos + 1
+  var buf = c.buf
+  var quote = buf[pos-1]
+  while true: 
+    case buf[pos]
+    of '\\':
+      c.bufpos = pos
+      getEscapedChar(c, tok)
+      pos = c.bufpos
+    of '\c', '\L', '\0':
+      tok.kind = tkInvalid
+      break
+    elif buf[pos] == quote:
+      inc(pos)
+      break      
+    else:
+      add(tok.literal, buf[pos])
+      Inc(pos)
+  c.bufpos = pos
+  
+proc getDollar(c: var TPegLexer, tok: var TToken) = 
+  var pos = c.bufPos + 1
+  var buf = c.buf
+  if buf[pos] in {'0'..'9'}:
+    tok.kind = tkBackref
+    tok.index = 0
+    while buf[pos] in {'0'..'9'}:
+      tok.index = tok.index * 10 + ord(buf[pos]) - ord('0')
+      inc(pos)
+  else:
+    tok.kind = tkDollar
+  c.bufpos = pos
+  
+proc getCharSet(c: var TPegLexer, tok: var TToken) = 
+  tok.kind = tkCharSet
+  tok.charset = {}
+  var pos = c.bufPos + 1
+  var buf = c.buf
+  var caret = false
+  if buf[pos] == '^':
+    inc(pos)
+    caret = true
+  while true:
+    var ch: char
+    case buf[pos]
+    of ']':
+      inc(pos)
+      break
+    of '\\':
+      c.bufpos = pos
+      getEscapedChar(c, tok)
+      pos = c.bufpos
+      ch = tok.literal[tok.literal.len-1]
+    of '\C', '\L', '\0':
+      tok.kind = tkInvalid
+      break
+    else: 
+      ch = buf[pos]
+      Inc(pos)
+    incl(tok.charset, ch)
+    if buf[pos] == '-':
+      if buf[pos+1] == ']':
+        incl(tok.charset, '-')
+        inc(pos)
+      else:
+        inc(pos)
+        var ch2: char
+        case buf[pos]
+        of '\\':
+          c.bufpos = pos
+          getEscapedChar(c, tok)
+          pos = c.bufpos
+          ch2 = tok.literal[tok.literal.len-1]
+        of '\C', '\L', '\0':
+          tok.kind = tkInvalid
+          break
+        else: 
+          ch2 = buf[pos]
+          Inc(pos)
+        for i in ord(ch)+1 .. ord(ch2):
+          incl(tok.charset, chr(i))
+  c.bufpos = pos
+  if caret: tok.charset = {'\1'..'\xFF'} - tok.charset
+  
+proc getSymbol(c: var TPegLexer, tok: var TToken) = 
+  var pos = c.bufpos
+  var buf = c.buf
+  while true: 
+    add(tok.literal, buf[pos])
+    Inc(pos)
+    if buf[pos] notin strutils.IdentChars: break
+  c.bufpos = pos
+  tok.kind = tkIdentifier
+
+proc getBuiltin(c: var TPegLexer, tok: var TToken) =
+  if c.buf[c.bufpos+1] in strutils.Letters:
+    inc(c.bufpos)
+    getSymbol(c, tok)
+    tok.kind = tkBuiltin
+  else:
+    tok.kind = tkEscaped
+    getEscapedChar(c, tok) # may set tok.kind to tkInvalid
+
+proc getTok(c: var TPegLexer, tok: var TToken) = 
+  tok.kind = tkInvalid
+  tok.modifier = modNone
+  setlen(tok.literal, 0)
+  skip(c)
+  case c.buf[c.bufpos]
+  of '{':
+    inc(c.bufpos)
+    if c.buf[c.bufpos] == '@' and c.buf[c.bufpos+1] == '}':
+      tok.kind = tkCurlyAt
+      inc(c.bufpos, 2)
+      add(tok.literal, "{@}")
+    else:
+      tok.kind = tkCurlyLe
+      add(tok.literal, '{')
+  of '}': 
+    tok.kind = tkCurlyRi
+    inc(c.bufpos)
+    add(tok.literal, '}')
+  of '[': 
+    getCharset(c, tok)
+  of '(':
+    tok.kind = tkParLe
+    Inc(c.bufpos)
+    add(tok.literal, '(')
+  of ')':
+    tok.kind = tkParRi
+    Inc(c.bufpos)
+    add(tok.literal, ')')
+  of '.': 
+    tok.kind = tkAny
+    inc(c.bufpos)
+    add(tok.literal, '.')
+  of '_':
+    tok.kind = tkAnyRune
+    inc(c.bufpos)
+    add(tok.literal, '_')
+  of '\\': 
+    getBuiltin(c, tok)
+  of '\'', '"': getString(c, tok)
+  of '$': getDollar(c, tok)
+  of '\0': 
+    tok.kind = tkEof
+    tok.literal = "[EOF]"
+  of 'a'..'z', 'A'..'Z', '\128'..'\255':
+    getSymbol(c, tok)
+    if c.buf[c.bufpos] in {'\'', '"'} or 
+        c.buf[c.bufpos] == '$' and c.buf[c.bufpos+1] in {'0'..'9'}:
+      case tok.literal
+      of "i": tok.modifier = modIgnoreCase
+      of "y": tok.modifier = modIgnoreStyle
+      of "v": tok.modifier = modVerbatim
+      else: nil
+      setLen(tok.literal, 0)
+      if c.buf[c.bufpos] == '$':
+        getDollar(c, tok)
+      else:
+        getString(c, tok)
+      if tok.modifier == modNone: tok.kind = tkInvalid
+  of '+':
+    tok.kind = tkPlus
+    inc(c.bufpos)
+    add(tok.literal, '+')
+  of '*':
+    tok.kind = tkStar
+    inc(c.bufpos)
+    add(tok.literal, '+')
+  of '<':
+    if c.buf[c.bufpos+1] == '-':
+      inc(c.bufpos, 2)
+      tok.kind = tkArrow
+      add(tok.literal, "<-")
+    else:
+      add(tok.literal, '<')
+  of '/':
+    tok.kind = tkBar
+    inc(c.bufpos)
+    add(tok.literal, '/')
+  of '?':
+    tok.kind = tkOption
+    inc(c.bufpos)
+    add(tok.literal, '?')
+  of '!':
+    tok.kind = tkNot
+    inc(c.bufpos)
+    add(tok.literal, '!')
+  of '&':
+    tok.kind = tkAmp
+    inc(c.bufpos)
+    add(tok.literal, '!')
+  of '@':
+    tok.kind = tkAt
+    inc(c.bufpos)
+    add(tok.literal, '@')
+    if c.buf[c.bufpos] == '@': 
+      tok.kind = tkCurlyAt
+      inc(c.bufpos)
+      add(tok.literal, '@')
+  of '^':
+    tok.kind = tkHat
+    inc(c.bufpos)
+    add(tok.literal, '^')
+  else:
+    add(tok.literal, c.buf[c.bufpos])
+    inc(c.bufpos)
+
+proc arrowIsNextTok(c: TPegLexer): bool =
+  # the only look ahead we need
+  var pos = c.bufpos
+  while c.buf[pos] in {'\t', ' '}: inc(pos)
+  result = c.buf[pos] == '<' and c.buf[pos+1] == '-'
+
+# ----------------------------- parser ----------------------------------------
+    
+type
+  EInvalidPeg* = object of EInvalidValue ## raised if an invalid
+                                         ## PEG has been detected
+  TPegParser = object of TPegLexer ## the PEG parser object
+    tok: TToken
+    nonterms: seq[PNonTerminal]
+    modifier: TModifier
+    captures: int
+    identIsVerbatim: bool
+    skip: TPeg
+
+proc pegError(p: TPegParser, msg: string, line = -1, col = -1) =
+  var e: ref EInvalidPeg
+  new(e)
+  e.msg = errorStr(p, msg, line, col)
+  raise e
+
+proc getTok(p: var TPegParser) = 
+  getTok(p, p.tok)
+  if p.tok.kind == tkInvalid: pegError(p, "invalid token")
+
+proc eat(p: var TPegParser, kind: TTokKind) =
+  if p.tok.kind == kind: getTok(p)
+  else: pegError(p, tokKindToStr[kind] & " expected")
+
+proc parseExpr(p: var TPegParser): TPeg
+
+proc getNonTerminal(p: var TPegParser, name: string): PNonTerminal =
+  for i in 0..high(p.nonterms):
+    result = p.nonterms[i]
+    if cmpIgnoreStyle(result.name, name) == 0: return
+  # forward reference:
+  result = newNonTerminal(name, getLine(p), getColumn(p))
+  add(p.nonterms, result)
+
+proc modifiedTerm(s: string, m: TModifier): TPeg =
+  case m
+  of modNone, modVerbatim: result = term(s)
+  of modIgnoreCase: result = termIgnoreCase(s)
+  of modIgnoreStyle: result = termIgnoreStyle(s)
+
+proc modifiedBackref(s: int, m: TModifier): TPeg =
+  case m
+  of modNone, modVerbatim: result = backRef(s)
+  of modIgnoreCase: result = backRefIgnoreCase(s)
+  of modIgnoreStyle: result = backRefIgnoreStyle(s)
+
+proc builtin(p: var TPegParser): TPeg =
+  # do not use "y", "skip" or "i" as these would be ambiguous
+  case p.tok.literal
+  of "n": result = newLine()
+  of "d": result = charset({'0'..'9'})
+  of "D": result = charset({'\1'..'\xff'} - {'0'..'9'})
+  of "s": result = charset({' ', '\9'..'\13'})
+  of "S": result = charset({'\1'..'\xff'} - {' ', '\9'..'\13'})
+  of "w": result = charset({'a'..'z', 'A'..'Z', '_', '0'..'9'})
+  of "W": result = charset({'\1'..'\xff'} - {'a'..'z','A'..'Z','_','0'..'9'})
+  of "a": result = charset({'a'..'z', 'A'..'Z'})
+  of "A": result = charset({'\1'..'\xff'} - {'a'..'z', 'A'..'Z'})
+  of "ident": result = tpegs.ident
+  of "letter": result = UnicodeLetter()
+  of "upper": result = UnicodeUpper()
+  of "lower": result = UnicodeLower()
+  of "title": result = UnicodeTitle()
+  of "white": result = UnicodeWhitespace()
+  else: pegError(p, "unknown built-in: " & p.tok.literal)
+
+proc token(terminal: TPeg, p: TPegParser): TPeg = 
+  if p.skip.kind == pkEmpty: result = terminal
+  else: result = sequence(p.skip, terminal)
+
+proc primary(p: var TPegParser): TPeg =
+  case p.tok.kind
+  of tkAmp:
+    getTok(p)
+    return &primary(p)
+  of tkNot:
+    getTok(p)
+    return !primary(p)
+  of tkAt:
+    getTok(p)
+    return !*primary(p)
+  of tkCurlyAt:
+    getTok(p)
+    return !*\primary(p).token(p)
+  else: nil
+  case p.tok.kind
+  of tkIdentifier:
+    if p.identIsVerbatim: 
+      var m = p.tok.modifier
+      if m == modNone: m = p.modifier
+      result = modifiedTerm(p.tok.literal, m).token(p)
+      getTok(p)
+    elif not arrowIsNextTok(p):
+      var nt = getNonTerminal(p, p.tok.literal)
+      incl(nt.flags, ntUsed)
+      result = nonTerminal(nt).token(p)
+      getTok(p)
+    else:
+      pegError(p, "expression expected, but found: " & p.tok.literal)
+  of tkStringLit:
+    var m = p.tok.modifier
+    if m == modNone: m = p.modifier
+    result = modifiedTerm(p.tok.literal, m).token(p)
+    getTok(p)
+  of tkCharSet:
+    if '\0' in p.tok.charset:
+      pegError(p, "binary zero ('\\0') not allowed in character class")
+    result = charset(p.tok.charset).token(p)
+    getTok(p)
+  of tkParLe:
+    getTok(p)
+    result = parseExpr(p)
+    eat(p, tkParRi)
+  of tkCurlyLe:
+    getTok(p)
+    result = capture(parseExpr(p)).token(p)
+    eat(p, tkCurlyRi)
+    inc(p.captures)
+  of tkAny:
+    result = any().token(p)
+    getTok(p)
+  of tkAnyRune:
+    result = anyRune().token(p)
+    getTok(p)
+  of tkBuiltin:
+    result = builtin(p).token(p)
+    getTok(p)
+  of tkEscaped:
+    result = term(p.tok.literal[0]).token(p)
+    getTok(p)
+  of tkDollar: 
+    result = endAnchor()
+    getTok(p)
+  of tkHat: 
+    result = startAnchor()
+    getTok(p)
+  of tkBackref:
+    var m = p.tok.modifier
+    if m == modNone: m = p.modifier
+    result = modifiedBackRef(p.tok.index, m).token(p)
+    if p.tok.index < 0 or p.tok.index > p.captures: 
+      pegError(p, "invalid back reference index: " & $p.tok.index)
+    getTok(p)
+  else:
+    pegError(p, "expression expected, but found: " & p.tok.literal)
+    getTok(p) # we must consume a token here to prevent endless loops!
+  while true:
+    case p.tok.kind
+    of tkOption:
+      result = ?result
+      getTok(p)
+    of tkStar:
+      result = *result
+      getTok(p)
+    of tkPlus:
+      result = +result
+      getTok(p)
+    else: break
+
+proc seqExpr(p: var TPegParser): TPeg =
+  result = primary(p)
+  while true:
+    case p.tok.kind
+    of tkAmp, tkNot, tkAt, tkStringLit, tkCharset, tkParLe, tkCurlyLe,
+       tkAny, tkAnyRune, tkBuiltin, tkEscaped, tkDollar, tkBackref, 
+       tkHat, tkCurlyAt:
+      result = sequence(result, primary(p))
+    of tkIdentifier:
+      if not arrowIsNextTok(p):
+        result = sequence(result, primary(p))
+      else: break
+    else: break
+
+proc parseExpr(p: var TPegParser): TPeg =
+  result = seqExpr(p)
+  while p.tok.kind == tkBar:
+    getTok(p)
+    result = result / seqExpr(p)
+  
+proc parseRule(p: var TPegParser): PNonTerminal =
+  if p.tok.kind == tkIdentifier and arrowIsNextTok(p):
+    result = getNonTerminal(p, p.tok.literal)
+    if ntDeclared in result.flags:
+      pegError(p, "attempt to redefine: " & result.name)
+    result.line = getLine(p)
+    result.col = getColumn(p)
+    getTok(p)
+    eat(p, tkArrow)
+    result.rule = parseExpr(p)
+    incl(result.flags, ntDeclared) # NOW inlining may be attempted
+  else:
+    pegError(p, "rule expected, but found: " & p.tok.literal)
+  
+proc rawParse(p: var TPegParser): TPeg =
+  ## parses a rule or a PEG expression
+  while p.tok.kind == tkBuiltin:
+    case p.tok.literal
+    of "i":
+      p.modifier = modIgnoreCase
+      getTok(p)
+    of "y":
+      p.modifier = modIgnoreStyle
+      getTok(p)
+    of "skip":
+      getTok(p)
+      p.skip = ?primary(p)
+    else: break
+  if p.tok.kind == tkIdentifier and arrowIsNextTok(p):
+    result = parseRule(p).rule
+    while p.tok.kind != tkEof:
+      discard parseRule(p)
+  else:
+    p.identIsVerbatim = true
+    result = parseExpr(p)
+  if p.tok.kind != tkEof:
+    pegError(p, "EOF expected, but found: " & p.tok.literal)
+  for i in 0..high(p.nonterms):
+    var nt = p.nonterms[i]
+    if ntDeclared notin nt.flags:
+      pegError(p, "undeclared identifier: " & nt.name, nt.line, nt.col)
+    elif ntUsed notin nt.flags and i > 0:
+      pegError(p, "unused rule: " & nt.name, nt.line, nt.col)
+
+proc parsePeg*(pattern: string, filename = "pattern", line = 1, col = 0): TPeg =
+  ## constructs a TPeg object from `pattern`. `filename`, `line`, `col` are
+  ## used for error messages, but they only provide start offsets. `parsePeg`
+  ## keeps track of line and column numbers within `pattern`.
+  var p: TPegParser
+  init(TPegLexer(p), pattern, filename, line, col)
+  p.tok.kind = tkInvalid
+  p.tok.modifier = modNone
+  p.tok.literal = ""
+  p.tok.charset = {}
+  p.nonterms = @[]
+  p.identIsVerbatim = false
+  getTok(p)
+  result = rawParse(p)
+
+proc peg*(pattern: string): TPeg =
+  ## constructs a TPeg object from the `pattern`. The short name has been
+  ## chosen to encourage its use as a raw string modifier::
+  ##
+  ##   peg"{\ident} \s* '=' \s* {.*}"
+  result = parsePeg(pattern, "pattern")
+
+proc escapePeg*(s: string): string = 
+  ## escapes `s` so that it is matched verbatim when used as a peg.
+  result = ""
+  var inQuote = false
+  for c in items(s):  
+    case c
+    of '\0'..'\31', '\'', '"', '\\': 
+      if inQuote: 
+        result.add('\'')
+        inQuote = false
+      result.add("\\x")
+      result.add(toHex(ord(c), 2))
+    else:
+      if not inQuote: 
+        result.add('\'')
+        inQuote = true
+      result.add(c)
+  if inQuote: result.add('\'')
+
+when isMainModule:
+  doAssert escapePeg("abc''def'") == r"'abc'\x27\x27'def'\x27"
+  #doAssert match("(a b c)", peg"'(' @ ')'")
+  doAssert match("W_HI_Le", peg"\y 'while'")
+  doAssert(not match("W_HI_L", peg"\y 'while'"))
+  doAssert(not match("W_HI_Le", peg"\y v'while'"))
+  doAssert match("W_HI_Le", peg"y'while'")
+  
+  doAssert($ +digits == $peg"\d+")
+  doAssert "0158787".match(peg"\d+")
+  doAssert "ABC 0232".match(peg"\w+\s+\d+")
+  doAssert "ABC".match(peg"\d+ / \w+")
+
+  for word in split("00232this02939is39an22example111", peg"\d+"):
+    writeln(stdout, word)
+
+  doAssert matchLen("key", ident) == 3
+
+  var pattern = sequence(ident, *whitespace, term('='), *whitespace, ident)
+  doAssert matchLen("key1=  cal9", pattern) == 11
+  
+  var ws = newNonTerminal("ws", 1, 1)
+  ws.rule = *whitespace
+  
+  var expr = newNonTerminal("expr", 1, 1)
+  expr.rule = sequence(capture(ident), *sequence(
+                nonterminal(ws), term('+'), nonterminal(ws), nonterminal(expr)))
+  
+  var c: TCaptures
+  var s = "a+b +  c +d+e+f"
+  doAssert rawMatch(s, expr.rule, 0, c) == len(s)
+  var a = ""
+  for i in 0..c.ml-1:
+    a.add(substr(s, c.matches[i][0], c.matches[i][1]))
+  doAssert a == "abcdef"
+  #echo expr.rule
+
+  #const filename = "lib/devel/peg/grammar.txt"
+  #var grammar = parsePeg(newFileStream(filename, fmRead), filename)
+  #echo "a <- [abc]*?".match(grammar)
+  doAssert find("_____abc_______", term("abc"), 2) == 5
+  doAssert match("_______ana", peg"A <- 'ana' / . A")
+  doAssert match("abcs%%%", peg"A <- ..A / .A / '%'")
+
+  if "abc" =~ peg"{'a'}'bc' 'xyz' / {\ident}":
+    doAssert matches[0] == "abc"
+  else:
+    doAssert false
+  
+  var g2 = peg"""S <- A B / C D
+                 A <- 'a'+
+                 B <- 'b'+
+                 C <- 'c'+
+                 D <- 'd'+
+              """
+  doAssert($g2 == "((A B) / (C D))")
+  doAssert match("cccccdddddd", g2)
+  doAssert("var1=key; var2=key2".replacef(peg"{\ident}'='{\ident}", "$1<-$2$2") ==
+         "var1<-keykey; var2<-key2key2")
+  doAssert "var1=key; var2=key2".endsWith(peg"{\ident}'='{\ident}")
+
+  if "aaaaaa" =~ peg"'aa' !. / ({'a'})+":
+    doAssert matches[0] == "a"
+  else:
+    doAssert false
+  
+  block:
+    var matches: array[0..2, string]
+    if match("abcdefg", peg"c {d} ef {g}", matches, 2): 
+      doAssert matches[0] == "d"
+      doAssert matches[1] == "g"
+    else:
+      doAssert false
+
+  for x in findAll("abcdef", peg"{.}", 3):
+    echo x
+    
+  if "f(a, b)" =~ peg"{[0-9]+} / ({\ident} '(' {@} ')')":
+    doAssert matches[0] == "f"
+    doAssert matches[1] == "a, b"
+  else:
+    doAssert false
+  
+  doAssert match("eine übersicht und außerdem", peg"(\letter \white*)+")
+  # ß is not a lower cased letter?!
+  doAssert match("eine übersicht und auerdem", peg"(\lower \white*)+")
+  doAssert match("EINE ÃœBERSICHT UND AUSSERDEM", peg"(\upper \white*)+")
+  doAssert(not match("456678", peg"(\letter)+"))
+
+  doAssert("var1 = key; var2 = key2".replacef(
+    peg"\skip(\s*) {\ident}'='{\ident}", "$1<-$2$2") ==
+         "var1<-keykey;var2<-key2key2")
+
+  doAssert match("prefix/start", peg"^start$", 7)
+  
+  # tricky test to check for false aliasing:
+  block:
+    var a = term"key"
+    echo($sequence(sequence(a, term"value"), *a))
+
diff --git a/tests/stdlib/tposix.nim b/tests/stdlib/tposix.nim
new file mode 100644
index 000000000..bf0b49586
--- /dev/null
+++ b/tests/stdlib/tposix.nim
@@ -0,0 +1,16 @@
+# Test Posix interface
+
+when not defined(windows):
+
+  import posix
+
+  var
+    u: Tutsname
+
+  discard uname(u)
+
+  writeln(stdout, u.sysname)
+  writeln(stdout, u.nodename)
+  writeln(stdout, u.release)
+  writeln(stdout, u.machine)
+
diff --git a/tests/stdlib/tquit.nim b/tests/stdlib/tquit.nim
new file mode 100644
index 000000000..d4dc1522d
--- /dev/null
+++ b/tests/stdlib/tquit.nim
@@ -0,0 +1,6 @@
+# Test the new beforeQuit variable: 

+

+proc myExit() {.noconv.} = 

+  write(stdout, "just exiting...\n")

+

+addQuitProc(myExit)

diff --git a/tests/stdlib/tregex.nim b/tests/stdlib/tregex.nim
new file mode 100644
index 000000000..bb4695f02
--- /dev/null
+++ b/tests/stdlib/tregex.nim
@@ -0,0 +1,31 @@
+discard """
+  file: "tregex.nim"
+  output: "key: keyAYes!"
+"""
+# Test the new regular expression module

+# which is based on the PCRE library

+
+when defined(powerpc64):
+  # cheat as our powerpc test machine has no PCRE installed:
+  echo "key: keyAYes!"
+
+else:

+  import

+    re

+

+  if "keyA = valueA" =~ re"\s*(\w+)\s*\=\s*(\w+)":

+    write(stdout, "key: ", matches[0])

+  elif "# comment!" =~ re.re"\s*(\#.*)": 

+    # test re.re"" syntax

+    echo("comment: ", matches[0])

+  else: 

+    echo("Bug!")

+

+  if "Username".match(re"[A-Za-z]+"):

+    echo("Yes!")

+  else:

+    echo("Bug!")

+

+  #OUT key: keyAYes!

+
+
diff --git a/tests/stdlib/treguse.nim b/tests/stdlib/treguse.nim
new file mode 100644
index 000000000..a610ad725
--- /dev/null
+++ b/tests/stdlib/treguse.nim
@@ -0,0 +1,27 @@
+discard """
+  file: "treguse.nim"
+  output: "055this should be the casehugh"
+"""
+# Test the register usage of the virtual machine and

+# the blocks in var statements

+

+proc main(a, b: int) =

+  var x = 0

+  write(stdout, x)

+  if x == 0:

+    var y = 55

+    write(stdout, y)

+    write(stdout, "this should be the case")

+    var input = "<no input>"

+    if input == "Andreas":

+      write(stdout, "wow")

+    else:

+      write(stdout, "hugh")

+  else:

+    var z = 66

+    write(stdout, z) # "bug!")

+

+main(45, 1000)

+#OUT 055this should be the casehugh

+
+
diff --git a/tests/stdlib/trepr.nim b/tests/stdlib/trepr.nim
new file mode 100644
index 000000000..41c830621
--- /dev/null
+++ b/tests/stdlib/trepr.nim
@@ -0,0 +1,29 @@
+discard """
+  file: "trepr.nim"
+  output: "{a, b}{'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'}"
+"""
+
+type
+  TEnum = enum
+    a, b
+    
+var val = {a, b}
+stdout.write(repr(val))
+stdout.writeln(repr({'a'..'z', 'A'..'Z'}))
+
+type
+  TObj {.pure, inheritable.} = object
+    data: int
+  TFoo = ref object of TObj
+    d2: float
+var foo: TFoo
+new(foo)
+
+when false:
+  # cannot capture this output as it contains a memory address :-/
+  echo foo.repr
+#var testseq: seq[string] = @[
+#  "a", "b", "c", "d", "e"
+#]
+#echo(repr(testseq))
+
diff --git a/tests/stdlib/trepr2.nim b/tests/stdlib/trepr2.nim
new file mode 100644
index 000000000..b15081e48
--- /dev/null
+++ b/tests/stdlib/trepr2.nim
@@ -0,0 +1,32 @@
+# test the new "repr" built-in proc

+

+type

+  TEnum = enum

+    en1, en2, en3, en4, en5, en6

+

+  TPoint {.final.} = object

+    x, y, z: int

+    s: array [0..1, string]

+    e: TEnum

+

+var

+  p: TPoint

+  q: ref TPoint

+  s: seq[ref TPoint]

+

+p.x = 0

+p.y = 13

+p.z = 45

+p.s[0] = "abc"

+p.s[1] = "xyz"

+p.e = en6

+

+new(q)

+q[] = p

+

+s = @[q, q, q, q]

+

+writeln(stdout, repr(p))

+writeln(stdout, repr(q))

+writeln(stdout, repr(s))

+writeln(stdout, repr(en4))

diff --git a/tests/stdlib/tsockets.nim b/tests/stdlib/tsockets.nim
new file mode 100644
index 000000000..6078504f5
--- /dev/null
+++ b/tests/stdlib/tsockets.nim
@@ -0,0 +1,11 @@
+import sockets
+var s: TSocket
+s = socket()
+
+s.connect("www.google.com", TPort(80))
+
+var data: string = ""
+s.readLine(data)
+echo(data)
+
+
diff --git a/tests/stdlib/tsortcall.nim b/tests/stdlib/tsortcall.nim
new file mode 100644
index 000000000..efe1d0b8b
--- /dev/null
+++ b/tests/stdlib/tsortcall.nim
@@ -0,0 +1,5 @@
+import algorithm
+
+proc foosort(ships: var seq[int]) = sort(ships, system.cmp[int])
+
+
diff --git a/tests/stdlib/tsplit.nim b/tests/stdlib/tsplit.nim
new file mode 100644
index 000000000..25bad33e2
--- /dev/null
+++ b/tests/stdlib/tsplit.nim
@@ -0,0 +1,20 @@
+discard """
+  file: "tsplit.nim"
+  output: "true"
+"""
+import strutils
+
+var s = ""
+for w in split("|abc|xy|z", {'|'}):
+  s.add("#")
+  s.add(w)
+
+if s == "#abc#xy#z":
+  echo "true"
+else:
+  echo "false"
+  
+#OUT true
+
+
+
diff --git a/tests/stdlib/tstreams.nim b/tests/stdlib/tstreams.nim
new file mode 100644
index 000000000..640565a27
--- /dev/null
+++ b/tests/stdlib/tstreams.nim
@@ -0,0 +1,7 @@
+import streams
+
+var outp = newFileStream(stdout)
+var inp = newFileStream(stdin)
+write(outp, "Hello! What is your name?")
+var line = readLine(inp)
+write(outp, "Nice name: " & line)
diff --git a/tests/stdlib/tstrset.nim b/tests/stdlib/tstrset.nim
new file mode 100644
index 000000000..9cdb5ed35
--- /dev/null
+++ b/tests/stdlib/tstrset.nim
@@ -0,0 +1,74 @@
+# test a simple yet highly efficient set of strings
+
+type
+  TRadixNodeKind = enum rnLinear, rnFull, rnLeaf
+  PRadixNode = ref TRadixNode
+  TRadixNode = object {.inheritable.}
+    kind: TRadixNodeKind
+  TRadixNodeLinear = object of TRadixNode
+    len: int8
+    keys: array [0..31, char]
+    vals: array [0..31, PRadixNode]  
+  TRadixNodeFull = object of TRadixNode
+    b: array [char, PRadixNode]
+  TRadixNodeLeaf = object of TRadixNode
+    s: string
+  PRadixNodeLinear = ref TRadixNodeLinear
+  PRadixNodeFull = ref TRadixNodeFull
+  PRadixNodeLeaf = ref TRadixNodeLeaf
+    
+proc search(r: PRadixNode, s: string): PRadixNode =
+  var r = r
+  var i = 0
+  while r != nil:
+    case r.kind
+    of rnLinear:
+      var x = PRadixNodeLinear(r)
+      for j in 0..ze(x.len)-1:
+        if x.keys[j] == s[i]:
+          if s[i] == '\0': return r
+          r = x.vals[j]
+          inc(i)
+          break
+      break # character not found
+    of rnFull:
+      var x = PRadixNodeFull(r)
+      var y = x.b[s[i]]
+      if s[i] == '\0':
+        return if y != nil: r else: nil
+      r = y
+      inc(i)
+    of rnLeaf:
+      var x = PRadixNodeLeaf(r)
+      var j = 0
+      while true:
+        if x.s[j] != s[i]: return nil
+        if s[i] == '\0': return r
+        inc(j)
+        inc(i)
+
+proc contains*(r: PRadixNode, s: string): bool =
+  return search(r, s) != nil
+
+proc testOrincl*(r: var PRadixNode, s: string): bool =
+  nil
+    
+proc incl*(r: var PRadixNode, s: string) = discard testOrIncl(r, s)
+
+proc excl*(r: var PRadixNode, s: string) =
+  var x = search(r, s)
+  if x == nil: return
+  case x.kind
+  of rnLeaf: PRadixNodeLeaf(x).s = ""
+  of rnFull: PRadixNodeFull(x).b['\0'] = nil
+  of rnLinear:
+    var x = PRadixNodeLinear(x)
+    for i in 0..ze(x.len)-1:
+      if x.keys[i] == '\0':
+        swap(x.keys[i], x.keys[ze(x.len)-1])
+        dec(x.len)
+        break
+
+var
+  root: PRadixNode
+
diff --git a/tests/stdlib/tstrtabs.nim b/tests/stdlib/tstrtabs.nim
new file mode 100644
index 000000000..251ec77ef
--- /dev/null
+++ b/tests/stdlib/tstrtabs.nim
@@ -0,0 +1,12 @@
+import strtabs
+
+var tab = newStringTable({"key1": "val1", "key2": "val2"}, 
+                         modeStyleInsensitive)
+for i in 0..80:
+  tab["key_" & $i] = "value" & $i
+  
+for key, val in pairs(tab):
+  writeln(stdout, key, ": ", val)
+writeln(stdout, "length of table ", $tab.len)
+
+writeln(stdout, `%`("$key1 = $key2; ${PATH}", tab, {useEnvironment}))
diff --git a/tests/stdlib/tstrutil.nim b/tests/stdlib/tstrutil.nim
new file mode 100644
index 000000000..80c2f3870
--- /dev/null
+++ b/tests/stdlib/tstrutil.nim
@@ -0,0 +1,45 @@
+discard """
+  file: "tstrutil.nim"
+  output: "ha/home/a1xyz/usr/bin"
+"""
+# test the new strutils module

+

+import

+  strutils

+

+proc testStrip() =

+  write(stdout, strip("  ha  "))

+

+proc main() = 

+  testStrip()

+  for p in split("/home/a1:xyz:/usr/bin", {':'}):

+    write(stdout, p)

+
+proc testDelete = 
+  var s = "0123456789ABCDEFGH"
+  delete(s, 4, 5)
+  assert s == "01236789ABCDEFGH"
+  delete(s, s.len-1, s.len-1)
+  assert s == "01236789ABCDEFG"
+  delete(s, 0, 0)
+  assert s == "1236789ABCDEFG"
+
+testDelete()  
+    

+assert(insertSep($1000_000) == "1_000_000")
+assert(insertSep($232) == "232")
+assert(insertSep($12345, ',') == "12,345")
+assert(insertSep($0) == "0")
+

+assert(editDistance("prefix__hallo_suffix", "prefix__hallo_suffix") == 0)

+assert(editDistance("prefix__hallo_suffix", "prefix__hallo_suffi1") == 1)

+assert(editDistance("prefix__hallo_suffix", "prefix__HALLO_suffix") == 5)

+assert(editDistance("prefix__hallo_suffix", "prefix__ha_suffix") == 3)

+assert(editDistance("prefix__hallo_suffix", "prefix") == 14)

+assert(editDistance("prefix__hallo_suffix", "suffix") == 14)

+assert(editDistance("prefix__hallo_suffix", "prefix__hao_suffix") == 2)

+

+main()

+#OUT ha/home/a1xyz/usr/bin

+
+
diff --git a/tests/stdlib/ttime.nim b/tests/stdlib/ttime.nim
new file mode 100644
index 000000000..bad818816
--- /dev/null
+++ b/tests/stdlib/ttime.nim
@@ -0,0 +1,6 @@
+# test the new time module

+

+import

+  times

+

+write(stdout, $getTime())

diff --git a/tests/stdlib/tunidecode.nim b/tests/stdlib/tunidecode.nim
new file mode 100644
index 000000000..cb6589d60
--- /dev/null
+++ b/tests/stdlib/tunidecode.nim
@@ -0,0 +1,12 @@
+discard """
+  cmd: "nimrod cc --hints:on -d:embedUnidecodeTable $# $#"
+  output: "Ausserst"
+"""
+
+import unidecode
+
+loadUnidecodeTable("lib/pure/unidecode/unidecode.dat")
+
+#assert unidecode("\x53\x17\x4E\xB0") == "Bei Jing"
+echo unidecode("Äußerst")
+
diff --git a/tests/stdlib/twalker.nim b/tests/stdlib/twalker.nim
new file mode 100644
index 000000000..89e6c2b9d
--- /dev/null
+++ b/tests/stdlib/twalker.nim
@@ -0,0 +1,13 @@
+# iterate over all files with a given filter:

+

+import

+  "../../lib/pure/os.nim", ../../ lib / pure / times

+

+proc main(filter: string) =

+  for filename in walkFiles(filter):

+    writeln(stdout, filename)

+

+  for key, val in envPairs():

+    writeln(stdout, key & '=' & val)

+

+main("*.nim")

diff --git a/tests/stdlib/txmlgen.nim b/tests/stdlib/txmlgen.nim
new file mode 100644
index 000000000..917427abc
--- /dev/null
+++ b/tests/stdlib/txmlgen.nim
@@ -0,0 +1,12 @@
+discard """
+  file: "txmlgen.nim"
+  output: "<h1><a href=\"http://force7.de/nimrod\">Nimrod</a></h1>"
+"""
+import htmlgen
+
+var nim = "Nimrod"
+echo h1(a(href="http://force7.de/nimrod", nim))
+
+
+
+
diff --git a/tests/stdlib/txmltree.nim b/tests/stdlib/txmltree.nim
new file mode 100644
index 000000000..931871f15
--- /dev/null
+++ b/tests/stdlib/txmltree.nim
@@ -0,0 +1,13 @@
+discard """
+  file: "txmltree.nim"
+  output: "true"
+"""
+
+import xmltree, strtabs
+
+var x = <>a(href="nimrod.de", newText("www.nimrod-test.de"))
+
+echo($x == "<a href=\"nimrod.de\">www.nimrod-test.de</a>")
+
+
+