Skip to content

Commit

Permalink
Fix highlighting for array of table in TOML (#1926)
Browse files Browse the repository at this point in the history
  • Loading branch information
fox0430 authored Nov 16, 2023
1 parent fcd518c commit 52302cd
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.. _#1926: https://github.com/fox0430/moe/pull/1926

Fixed
.....

- `#1926`_ Fix highlighting for array of table in TOML

24 changes: 20 additions & 4 deletions src/moepkg/syntax/syntaxtoml.nim
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,25 @@ proc tomlNumberAndDate(g: var GeneralTokenizer, position: int): int =

return pos

proc tomlTable(g: var GeneralTokenizer, position: int): int =
var pos = position

g.kind = gtTable

while not (g.buf[pos] in {'\n', '\r', '\0', '[', '['}):
inc(pos)

if g.buf[pos] == '[':
# Array of table
var countClose = 0
while not (g.buf[pos] in {'\n', '\r', '\0'} or countClose == 2):
inc(pos)
if g.buf[pos] == ']': countClose.inc

if countClose == 2: pos.inc

return pos

proc tomlNextToken*(g: var GeneralTokenizer) =
var pos = g.pos
g.start = g.pos
Expand Down Expand Up @@ -133,10 +152,7 @@ proc tomlNextToken*(g: var GeneralTokenizer) =
pos = tomlNumberAndDate(g, pos)
if g.buf[pos] in Letters: inc(pos)
of '[':
g.kind = gtTable
while g.buf[pos] != ']' and not (g.buf[pos] in {'\n', '\r', '\0'}):
inc(pos)
if g.buf[pos] == ']': inc(pos)
pos = tomlTable(g, pos)
of '\"', '\'':
inc(pos)
g.kind = gtStringLit
Expand Down
26 changes: 25 additions & 1 deletion tests/tsyntaxtoml.nim
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ proc tokens(code: string): seq[GT] =
result[^1].buf = ""

suite "syntax: Toml":

test "Basic":
const Code = """
[table]
Expand Down Expand Up @@ -501,3 +500,28 @@ str2 = '''Here are two quotation marks: "". Simple enough.'''
check tokens(Code) == @[
GT(kind: gtTable, start: 0, length: 2, buf: "", pos: 2, state: gtEof)
]

test "Array of table":
const Code = "[[table]]"
check tokens(Code) == @[
GT(kind: gtTable, start: 0, length: 9, buf: "", pos: 9, state: gtEof)
]

test "Incomplete array of table":
const Code = "[[table"
check tokens(Code) == @[
GT(kind: gtTable, start: 0, length: 7, buf: "", pos: 7, state: gtEof)
]

test "Incomplete array of table 2":
const Code = "[[table]"
check tokens(Code) == @[
GT(kind: gtTable, start: 0, length: 8, buf: "", pos: 8, state: gtEof)
]

test "Invalid array of table":
const Code = "[[[table]]]"
check tokens(Code) == @[
GT(kind: gtTable, start: 0, length: 10, buf: "", pos: 10, state: gtEof),
GT(kind: gtNone, start: 10, length: 1, buf: "", pos: 11, state: gtEof)
]

0 comments on commit 52302cd

Please sign in to comment.