Skip to content

Commit 2514bea

Browse files
committed
handle HTML comments [#22 - done]
Signed-off-by: George Lemon <georgelemon@protonmail.com>
1 parent d4030d5 commit 2514bea

3 files changed

Lines changed: 37 additions & 3 deletions

File tree

src/tim/engine/lexer.nim

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ type
4242
tkContinueCmd = "continue",
4343
tkIdentVar, tkIdentVarSafe,
4444
tkStatic, tkEcho = "echo",
45-
tkComment, tkDoc, tkNil = "nil",
45+
tkComment, tkDoc, tkHtmlComment, tkNil = "nil",
4646
tkUnknown
4747

4848
TokenTuple* = tuple
@@ -351,7 +351,25 @@ proc nextToken*(lex: var Lexer): TokenTuple =
351351
lex.advance()
352352
result = initToken(lex, tkGt, line, col, pos, wsno)
353353
of '<':
354-
if lex.peek() == '=':
354+
if lex.peek() == '!' and lex.peek(2) == '-' and lex.peek(3) == '-':
355+
# HTML comment <!-- ... -->
356+
lex.advance() # <
357+
lex.advance() # !
358+
lex.advance() # -
359+
lex.advance() # -
360+
lex.strbuf.setLen(0)
361+
while true:
362+
if lex.current == '-' and lex.peek() == '-' and lex.peek(2) == '>':
363+
lex.advance() # -
364+
lex.advance() # -
365+
lex.advance() # >
366+
break
367+
if lex.current == '\0':
368+
lex.error("EOF reached before closing --> for HTML comment")
369+
lex.strbuf.add(lex.current)
370+
lex.advance()
371+
result = initToken(lex, tkHtmlComment, move lex.strbuf, line, col, pos, wsno)
372+
elif lex.peek() == '=':
355373
lex.advance()
356374
lex.advance()
357375
result = initToken(lex, tkLte, "<=", line, col, pos, wsno)

src/tim/engine/parser.nim

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -660,6 +660,11 @@ prefixHandle parseRawHtml:
660660
result.rawHtml =move output
661661
walk p
662662

663+
prefixHandle parseHtmlComment:
664+
result = ast.newNode(nkRawHtml)
665+
result.rawHtml = "<!-- " & strip(p.curr.value) & " -->"
666+
walk p
667+
663668
#
664669
# Identifier & Variable Definitions
665670
#
@@ -1174,6 +1179,7 @@ proc getPrefixFn(p: var Parser, minPrec: int): PrefixFunction =
11741179
of tkSnippetJs: parseJavaScript
11751180
of tkSnippetCSS: parseCSS
11761181
of tkSnippetHtml: parseRawHtml
1182+
of tkHtmlComment: parseHtmlComment
11771183
of tkViewLoader: parseViewPlaceholder
11781184
of tkClient: parseClientBlock
11791185
else: nil
@@ -1309,6 +1315,7 @@ prefixHandle parseStmt:
13091315
of tkSnippetJs: parseJavaScript
13101316
of tkSnippetCSS: parseCSS
13111317
of tkSnippetHtml: parseRawHtml
1318+
of tkHtmlComment: parseHtmlComment
13121319
of tkViewLoader: parseViewPlaceholder
13131320
of tkClient: parseClientBlock
13141321
else: parseExpression

tests/test1.nim

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import std/[unittest, os, xmltree, strtabs,
2-
sequtils, json, options, htmlparser]
2+
sequtils, json, options, htmlparser, strutils]
33

44
include ../src/tim/engine/transformers
55
import pkg/vancode/interpreter/[ast, codegen, chunk, sym, vm, value, resolver]
@@ -110,3 +110,12 @@ p: "This will cause an error: " & $this["undefinedKey"]
110110
# this is not a code generation error, but a runtime error due to
111111
# accessing an undefined key in the JSON storage, so a KeyError is expected
112112
assert e.msg == "key not found: undefinedKey"
113+
114+
test "html comments":
115+
let samplecode = """
116+
div.container
117+
<!-- This is an HTML comment -->
118+
p: "Hello"
119+
"""
120+
let html = toHtml("test8", samplecode)
121+
doAssert "<!-- This is an HTML comment -->" in html

0 commit comments

Comments
 (0)