Skip to content

Commit e55412e

Browse files
committedSep 20, 2024·
fix: ignore comments while parsing unquoted strings
Signed-off-by: George Lemon <georgelemon@protonmail.com>
1 parent 8b69932 commit e55412e

File tree

1 file changed

+23
-10
lines changed

1 file changed

+23
-10
lines changed
 

‎src/nyml/parser.nim

+23-10
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,18 @@ handlers:
4040
lex.kind = tkString
4141
result = lex.kind
4242
break
43+
of '#':
44+
if lex.buf[lex.bufpos - 1] == ' ':
45+
lex.token = lex.token.strip
46+
inc lex.bufpos
47+
while true:
48+
case lex.buf[lex.bufpos]
49+
of EndOfFile, NewLines: break
50+
else:
51+
inc lex.bufpos
52+
else:
53+
add lex.token, lex.buf[lex.bufpos]
54+
inc lex.bufpos
4355
of ':':
4456
lex.kind = tkIdentifier
4557
result = lex.kind
@@ -363,16 +375,17 @@ proc parseUnquotedStrings(p: var Parser,
363375
proc parse(p: var Parser, inArray = false): Node
364376
proc parseObject(p: var Parser, this: TokenTuple, inArray = false): Node
365377

378+
template checkInlineString {.dirty.} =
379+
if p.next.kind notin {tkEOF, tkComment} and p.next.line == p.curr.line:
380+
let this = p.curr
381+
return p.parseUnquotedStrings(this, {tkComment})
382+
366383
proc parseString(p: var Parser): Node =
384+
checkInlineString()
367385
result = p.newNode String
368386
result.vStr = p.curr.value
369387
walk p
370388

371-
template checkInlineString {.dirty.} =
372-
if p.next.kind notin {tkEOF, tkComment} and p.next.line == p.curr.line:
373-
let this = p.curr
374-
return p.parseUnquotedStrings(this)
375-
376389
proc parseInt(p: var Parser): Node =
377390
checkInlineString()
378391
result = p.newNode Int
@@ -397,10 +410,10 @@ proc parseVariable(p: var Parser, this: TokenTuple, inArray = false): Node =
397410
walk p
398411
if inArray:
399412
if p.curr.line == this.line and p.curr.kind notin {tkComment, tkEOF, tkRB, tkComma}:
400-
result.varRight.add p.parseUnquotedStrings(this, {tkRB, tkComma})
413+
result.varRight.add p.parseUnquotedStrings(this, {tkRB, tkComma, tkComment})
401414
else:
402415
if p.curr.line == this.line and p.curr.kind notin {tkComment, tkEOF}:
403-
result.varRight.add p.parseUnquotedStrings(this)
416+
result.varRight.add p.parseUnquotedStrings(this, {tkComment})
404417

405418
proc parseArray(p: var Parser, node: Node, this: TokenTuple) =
406419
while p.curr.kind == tkHyphen and p.curr.pos == node.meta.pos:
@@ -412,7 +425,7 @@ proc parseArray(p: var Parser, node: Node, this: TokenTuple) =
412425
elif p.curr.kind == tkIdentifier:
413426
if p.next.kind != tkColon:
414427
# handle unquoted strings.
415-
node.items.add p.parseUnquotedStrings(this, {tkHyphen})
428+
node.items.add p.parseUnquotedStrings(this, {tkHyphen, tkComment})
416429
else:
417430
# handle objects
418431
let
@@ -450,7 +463,7 @@ proc parseInlineArray(p: var Parser, this: TokenTuple): Node =
450463
elif p.curr.kind == tkVariable:
451464
result.items.add p.parseVariable(this, true)
452465
else:
453-
result.items.add p.parseUnquotedStrings(this, {tkRB, tkComma}) # todo fails to find tkRB
466+
result.items.add p.parseUnquotedStrings(this, {tkRB, tkComma, tkComment}) # todo fails to find tkRB
454467
if p.curr.kind == tkComma: walk p
455468
walk p # ]
456469

@@ -527,7 +540,7 @@ proc parseObject(p: var Parser, this: TokenTuple, inArray = false): Node =
527540
else:
528541
result = p.newNode(Field, this)
529542
result.fieldKey = this.value
530-
result.fieldValue.add p.parseUnquotedStrings(this)
543+
result.fieldValue.add p.parseUnquotedStrings(this, {tkComment})
531544
else:
532545
result = p.newNode(Object, this)
533546
result.key = this.value

0 commit comments

Comments
 (0)
Please sign in to comment.