Skip to content

Commit

Permalink
improvements (#45)
Browse files Browse the repository at this point in the history
* improvements

* nifgram: use peekParRi
  • Loading branch information
Araq authored Sep 4, 2024
1 parent 8017852 commit c4eeac2
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 60 deletions.
5 changes: 4 additions & 1 deletion src/lib/nifcursors.nim
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ type

proc `=copy`(dest: var TokenBuf; src: TokenBuf) {.error.}
proc `=destroy`(dest: TokenBuf) {.inline.} =
assert dest.readers == 0, "TokenBuf still in use by some reader"
#assert dest.readers == 0, "TokenBuf still in use by some reader"
if dest.data != nil: dealloc(dest.data)

proc isMutable(b: TokenBuf): bool {.inline.} = b.cap >= 0
Expand Down Expand Up @@ -93,6 +93,9 @@ proc `[]`*(b: TokenBuf; i: int): PackedToken {.inline.} =
result = b.data[i]

proc add*(result: var TokenBuf; c: Cursor) =
result.add c.load

proc addSubtree*(result: var TokenBuf; c: Cursor) =
assert c.kind != ParRi, "cursor at end?"
if c.kind != ParLe:
# atom:
Expand Down
29 changes: 22 additions & 7 deletions src/nifgram/nifgram.nim
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ proc emitForLoop(c: var Context; it: string): string =
else:
result = it
ind c
c.outp.add "while not matchParRi(" & c.args & "):"
c.outp.add "while not peekParRi(" & c.args & "):"

proc compileZeroOrMany(c: var Context; it: string): string =
result = declTemp(c, "zm", "true")
Expand All @@ -118,7 +118,13 @@ proc compileZeroOrMany(c: var Context; it: string): string =
ind c
c.outp.add "if not "
c.outp.add cond
c.outp.add ": " & result & " = false"
c.outp.add ":"
inc c.nesting
ind c
c.outp.add result & " = false"
ind c
c.outp.add "break"
dec c.nesting
dec c.nesting

proc compileOneOrMany(c: var Context; it: string): string =
Expand All @@ -133,7 +139,13 @@ proc compileOneOrMany(c: var Context; it: string): string =
ind c
c.outp.add "if not "
c.outp.add cond
c.outp.add ": " & result & " = false"
c.outp.add ":"
inc c.nesting
ind c
c.outp.add result & " = false"
ind c
c.outp.add "break"
dec c.nesting
dec c.nesting

proc compileZeroOrOne(c: var Context; it: string): string =
Expand Down Expand Up @@ -161,7 +173,7 @@ proc compileKeyw(c: var Context; it: string): string =

let cond = "isTag(" & c.args & ", " & tagAsNimIdent(tag) & ")"
c.t = next(c.r)
if c.t.tk == ParRi:
if c.t.tk == ParRi and c.inMatch == 0:
return cond

let action = "handle" & upcase(tag)
Expand Down Expand Up @@ -228,9 +240,12 @@ proc compileKeyw(c: var Context; it: string): string =
c.outp.add result
c.outp.add ": "
c.outp.add action & "(" & c.args & ", " & before & ")"
dec c.nesting, 2
else:
dec c.nesting

dec c.nesting
ind c
c.outp.add "else: "
c.outp.add c.leaveBlock

proc compileRuleInvokation(c: var Context; it: string): string =
let ruleName = decodeStr(c.t)
Expand Down Expand Up @@ -423,7 +438,7 @@ proc compileMatch(c: var Context; it: string): string =

ind c
c.outp.add "block " & lab & ":"
c.leaveBlock = "rollback(" & c.args0 & ", " & before & "); break " & lab
c.leaveBlock = "(; rollback(" & c.args0 & ", " & before & "); break " & lab & ")"

inc c.nesting

Expand Down
Loading

0 comments on commit c4eeac2

Please sign in to comment.