diff --git a/README.md b/README.md index 748ca82..9b6d4fb 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,7 @@ parseCirru "a b" which returns: ```nim -(kind: cirruSeq, list: @[(kind: cirruSeq, list: @[(kind: cirruString, text: "a"), (kind: cirruString, text: "b")])]) : CirruNode +(kind: cirruList, list: @[(kind: cirruList, list: @[(kind: cirruToken, token: "a"), (kind: cirruToken, token: "b")])]) : CirruNode ``` `CirruNode` is the type of exprssions and tokens parsed from Cirru code. Browse [types.nim](src/cirru_parser/types.nim) for definitions. @@ -26,16 +26,16 @@ which returns: ```nim type CirruNodeKind* = enum - cirruString, - cirruSeq + cirruToken, + cirruList CirruNode* = object line*: int column*: int case kind*: CirruNodeKind - of cirruString: - text*: string - of cirruSeq: + of cirruToken: + token*: string + of cirruList: list*: DoublyLinkedList[CirruNode] ``` diff --git a/cirru_parser.nimble b/cirru_parser.nimble index ae2e923..8534543 100644 --- a/cirru_parser.nimble +++ b/cirru_parser.nimble @@ -1,6 +1,6 @@ # Package -version = "0.2.4" +version = "0.3.0" author = "jiyinyiyong" description = "Parser for Cirru syntax" license = "MIT" @@ -14,9 +14,9 @@ requires "nim >= 0.20.0" task t, "Runs the test suite": exec "nim c --hints:off -r tests/test_iterator" - # exec "nim c --hints:off -r tests/test_parser" - # exec "nim c --hints:off -r tests/test_lexer" - # exec "nim c --hints:off -r tests/test_types" + exec "nim c --hints:off -r tests/test_parser" + exec "nim c --hints:off -r tests/test_lexer" + exec "nim c --hints:off -r tests/test_types" task perf, "try large file": exec "nim compile --verbosity:0 --profiler:on --stackTrace:on --hints:off -r tests/parse_cost" diff --git a/src/cirru_parser.nim b/src/cirru_parser.nim index 30a6d65..ff7f75e 100644 --- a/src/cirru_parser.nim +++ b/src/cirru_parser.nim @@ -6,7 +6,7 @@ import cirru_parser/helpers import cirru_parser/lexer import cirru_parser/transformer -export CirruNode, CirruNodeKind, isSeq, isToken, `==`, `!=`, CirruParseError, formatParserFailure +export CirruNode, CirruNodeKind, isList, isToken, `==`, `!=`, CirruParseError, formatParserFailure export toCirru, toJson, items, `[]`, len, first, isEmpty, rest, restInLinkedList proc digestParsingParens*(tokens: var DoublyLinkedList[LexNode]): DoublyLinkedList[CirruNode] = @@ -23,15 +23,15 @@ proc digestParsingParens*(tokens: var DoublyLinkedList[LexNode]): DoublyLinkedLi case cursor.kind of lexToken: - exprs.append CirruNode(kind: cirruString, text: cursor.text, line: cursor.line, column: cursor.column) + exprs.append CirruNode(kind: cirruToken, token: cursor.token, line: cursor.line, column: cursor.column) tokens.remove tokens.head continue - of lexControl: + of lexOperator: case cursor.operator of controlParenOpen: tokens.remove tokens.head let children = digestParsingParens(tokens) - exprs.append CirruNode(kind: cirruSeq, list: children, line: cursor.line, column: cursor.column) + exprs.append CirruNode(kind: cirruList, list: children, line: cursor.line, column: cursor.column) continue of controlParenClose: tokens.remove tokens.head @@ -50,17 +50,17 @@ proc digestParsingIndentation*(tokens: var DoublyLinkedList[LexNode]): DoublyLin let cursor = tokens.head.value case cursor.kind of lexToken: - exprs.append CirruNode(kind: cirruString, text: cursor.text, line: cursor.line, column: cursor.column) + exprs.append CirruNode(kind: cirruToken, token: cursor.token, line: cursor.line, column: cursor.column) tokens.remove tokens.head continue - of lexControl: + of lexOperator: case cursor.operator of controlParenOpen: tokens.remove tokens.head if tokens.head.isNil: raiseParseExceptionAtNode("Wrong open paren here", cursor) let children = digestParsingParens(tokens) - exprs.append CirruNode(kind: cirruSeq, list: children, line: cursor.line, column: cursor.column) + exprs.append CirruNode(kind: cirruList, list: children, line: cursor.line, column: cursor.column) continue of controlParenClose: raiseParseExceptionAtNode("Unexpected paren close inside a line", cursor) @@ -68,7 +68,7 @@ proc digestParsingIndentation*(tokens: var DoublyLinkedList[LexNode]): DoublyLin of controlIndent: tokens.remove tokens.head let children = digestParsingIndentation(tokens) - exprs.append CirruNode(kind: cirruSeq, list: children, line: cursor.line, column: cursor.column) + exprs.append CirruNode(kind: cirruList, list: children, line: cursor.line, column: cursor.column) continue of controlOutdent: tokens.remove tokens.head @@ -83,20 +83,20 @@ proc parseCirru*(code: string): CirruNode {.exportc.} = # echo "tokens: ", tokens if tokens.head.isNil: - return CirruNode(kind: cirruSeq, list: initDoublyLinkedList[CirruNode](), line: 1, column: 0) + return CirruNode(kind: cirruList, list: initDoublyLinkedList[CirruNode](), line: 1, column: 0) let r0 = tokens.head.value let firstExpr = digestParsingIndentation(tokens) - lines.append CirruNode(kind: cirruSeq, list: firstExpr, line: r0.line, column: r0.column) + lines.append CirruNode(kind: cirruList, list: firstExpr, line: r0.line, column: r0.column) while tokens.head.isNil.not: let r0 = tokens.head.value - if r0.kind == lexControl and r0.operator == controlIndent: + if r0.kind == lexOperator and r0.operator == controlIndent: tokens.remove tokens.head let children = digestParsingIndentation(tokens) - lines.append CirruNode(kind: cirruSeq, list: children, line: r0.line, column: r0.column) + lines.append CirruNode(kind: cirruList, list: children, line: r0.line, column: r0.column) else: echo tokens raiseParseExceptionAtNode("Unexpected tokens sequence!", r0) - return resolveComma(resolveDollar(CirruNode(kind: cirruSeq, list: lines, line: 1, column: 0))) + return resolveComma(resolveDollar(CirruNode(kind: cirruList, list: lines, line: 1, column: 0))) diff --git a/src/cirru_parser/helpers.nim b/src/cirru_parser/helpers.nim index 873b233..38ae828 100644 --- a/src/cirru_parser/helpers.nim +++ b/src/cirru_parser/helpers.nim @@ -7,8 +7,8 @@ import options import ./types -proc createCirruString*(x: string): CirruNode = - return CirruNode(kind: cirruString, text: x) +proc createCirruToken*(x: string): CirruNode = + return CirruNode(kind: cirruToken, token: x) proc toCirru*(xs: JsonNode): CirruNode = case xs.kind: @@ -16,25 +16,25 @@ proc toCirru*(xs: JsonNode): CirruNode = var b: DoublyLinkedList[CirruNode] for k, v in xs.elems: b.append toCirru(v) - return CirruNode(kind: cirruSeq, list: b) + return CirruNode(kind: cirruList, list: b) of JString: - return CirruNode(kind: cirruString, text: xs.str) + return CirruNode(kind: cirruToken, token: xs.str) else: echo xs raiseParseException("Unknown type in JSON", 1, 0) proc toJson*(xs: CirruNode): JsonNode = case xs.kind: - of cirruString: - return JsonNode(kind: JString, str: xs.text) - of cirruSeq: + of cirruToken: + return JsonNode(kind: JString, str: xs.token) + of cirruList: return JsonNode(kind: JArray, elems: xs.list.toSeq.map(toJson)) -proc genLexToken*(text: string): LexNode = - return LexNode(kind: lexToken, text: text) +proc genLexToken*(x: string): LexNode = + return LexNode(kind: lexToken, token: x) -proc genLexControl*(operator: ControlOperator): LexNode = - return LexNode(kind: lexControl, operator: operator) +proc genLexOperator*(operator: ControlOperator): LexNode = + return LexNode(kind: lexOperator, operator: operator) proc formatParserFailure*(code, msg, filename: string, line, column: int): string = let lines = splitLines(code) @@ -47,13 +47,13 @@ proc formatParserFailure*(code, msg, filename: string, line, column: int): strin return "At " & filename & ":" & $line & ":" & $column & "\n" & $previousLine & failureLine & "\n" & spaces & "^ " & msg iterator items*(xs: CirruNode): CirruNode = - if xs.kind == cirruString: + if xs.kind == cirruToken: raise newException(ValueError, "Cannot create iterator on a cirru string") for child in xs.list: yield child proc `[]`*(xs: CirruNode, fromTo: HSlice[int, int]): seq[CirruNode] = - if xs.kind == cirruString: + if xs.kind == cirruToken: raise newException(ValueError, "Cannot create iterator on a cirru string") let fromA = fromTo.a @@ -64,7 +64,7 @@ proc `[]`*(xs: CirruNode, fromTo: HSlice[int, int]): seq[CirruNode] = result[idx] = xs[fromA + idx].get proc `[]`*(xs: CirruNode, fromTo: HSlice[int, BackwardsIndex]): seq[CirruNode] = - if xs.kind == cirruString: + if xs.kind == cirruToken: raise newException(ValueError, "Cannot create iterator on a cirru string") let fromA = fromTo.a diff --git a/src/cirru_parser/lexer.nim b/src/cirru_parser/lexer.nim index 642e9e0..0dfc5d9 100644 --- a/src/cirru_parser/lexer.nim +++ b/src/cirru_parser/lexer.nim @@ -16,7 +16,7 @@ proc lexCode*(code: string): DoublyLinkedList[LexNode] = proc digestBuffer(): void = if buffer.len > 0: - pieces.append LexNode(kind: lexToken, text: buffer, line: line, column: column) + pieces.append LexNode(kind: lexToken, token: buffer, line: line, column: column) buffer = "" proc digestIdentation(): void = @@ -30,18 +30,18 @@ proc lexCode*(code: string): DoublyLinkedList[LexNode] = # echo "indentation:", level if level > 0: for i in 1..level.int: - pieces.append LexNode(kind: lexControl, operator: controlIndent, line: line, column: column) + pieces.append LexNode(kind: lexOperator, operator: controlIndent, line: line, column: column) elif level < 0: for i in 1..(-level.int): - pieces.append LexNode(kind: lexControl, operator: controlOutdent, line: line, column: column) + pieces.append LexNode(kind: lexOperator, operator: controlOutdent, line: line, column: column) # special logic to generate extra newline ops - pieces.append LexNode(kind: lexControl, operator: controlOutdent, line: line, column: column) - pieces.append LexNode(kind: lexControl, operator: controlIndent, line: line, column: column) + pieces.append LexNode(kind: lexOperator, operator: controlOutdent, line: line, column: column) + pieces.append LexNode(kind: lexOperator, operator: controlIndent, line: line, column: column) else: if pieces.head.isNil.not: - pieces.append LexNode(kind: lexControl, operator: controlOutdent, line: line, column: column) - pieces.append LexNode(kind: lexControl, operator: controlIndent, line: line, column: column) + pieces.append LexNode(kind: lexOperator, operator: controlOutdent, line: line, column: column) + pieces.append LexNode(kind: lexOperator, operator: controlIndent, line: line, column: column) previousIndentation = indentation # echo "previousIndentation: ", previousIndentation @@ -73,7 +73,7 @@ proc lexCode*(code: string): DoublyLinkedList[LexNode] = lexingState = lexStateString of '(': digestIdentation() - pieces.append LexNode(kind: lexControl, operator: controlParenOpen, line: line, column: column) + pieces.append LexNode(kind: lexOperator, operator: controlParenOpen, line: line, column: column) lexingState = lexStateSpace of ')': raiseParseException("Unexpected ) in line head", line, column) @@ -97,7 +97,7 @@ proc lexCode*(code: string): DoublyLinkedList[LexNode] = of '"': lexingState = lexStateSpace # special case, add even if token is empty - pieces.append LexNode(kind: lexToken, text: buffer, line: line, column: column) + pieces.append LexNode(kind: lexToken, token: buffer, line: line, column: column) buffer = "" else: buffer.add c @@ -113,10 +113,10 @@ proc lexCode*(code: string): DoublyLinkedList[LexNode] = of '(': if buffer.strip().len > 0: digestBuffer() - pieces.append LexNode(kind: lexControl, operator: controlParenOpen, line: line, column: column) + pieces.append LexNode(kind: lexOperator, operator: controlParenOpen, line: line, column: column) lexingState = lexStateSpace of ')': - pieces.append LexNode(kind: lexControl, operator: controlParenClose, line: line, column: column) + pieces.append LexNode(kind: lexOperator, operator: controlParenClose, line: line, column: column) digestBuffer() lexingState = lexStateSpace else: @@ -130,11 +130,11 @@ proc lexCode*(code: string): DoublyLinkedList[LexNode] = of '(': if buffer.len > 0: digestBuffer() - pieces.append LexNode(kind: lexControl, operator: controlParenOpen, line: line, column: column) + pieces.append LexNode(kind: lexOperator, operator: controlParenOpen, line: line, column: column) lexingState = lexStateSpace of ')': digestBuffer() - pieces.append LexNode(kind: lexControl, operator: controlParenClose, line: line, column: column) + pieces.append LexNode(kind: lexOperator, operator: controlParenClose, line: line, column: column) lexingState = lexStateSpace of '\n': digestBuffer() @@ -144,7 +144,7 @@ proc lexCode*(code: string): DoublyLinkedList[LexNode] = case lexingState of lexStateToken: - pieces.append LexNode(kind: lexToken, text: buffer, line: line, column: column) + pieces.append LexNode(kind: lexToken, token: buffer, line: line, column: column) of lexStateEscape: raiseParseException("EOF at escape", line, column) of lexStateString: diff --git a/src/cirru_parser/transformer.nim b/src/cirru_parser/transformer.nim index 7b65ca8..e3abb2b 100644 --- a/src/cirru_parser/transformer.nim +++ b/src/cirru_parser/transformer.nim @@ -5,20 +5,20 @@ import ./types proc resolveDollar*(expr: CirruNode): CirruNode = case expr.kind - of cirruString: + of cirruToken: return expr - of cirruSeq: + of cirruList: # try to skip some children var hasDollar = false var hasList = false for child in expr.list: case child.kind - of cirruString: - if child.text == "$": + of cirruToken: + if child.token == "$": hasDollar = true break - of cirruSeq: + of cirruList: hasList = true break if not hasDollar and not hasList: @@ -28,35 +28,35 @@ proc resolveDollar*(expr: CirruNode): CirruNode = var k = 0 for child in expr.list: case child.kind - of cirruString: - if child.text == "$": - let following = resolveDollar(CirruNode(kind: cirruSeq, list: expr.list.copyFrom(k+1), line: child.line, column: child.column)) + of cirruToken: + if child.token == "$": + let following = resolveDollar(CirruNode(kind: cirruList, list: expr.list.copyFrom(k+1), line: child.line, column: child.column)) case following.kind - of cirruSeq: - buffer.append CirruNode(kind: cirruSeq, list: following.list, line: child.line, column: child.column) + of cirruList: + buffer.append CirruNode(kind: cirruList, list: following.list, line: child.line, column: child.column) break - of cirruString: - raiseParseException("Should not return cirruString", following.line, following.column) + of cirruToken: + raiseParseException("Should not return cirruToken", following.line, following.column) else: buffer.append child - of cirruSeq: + of cirruList: buffer.append resolveDollar(child) k += 1 - return CirruNode(kind: cirruSeq, list: buffer, line: expr.line, column: expr.column) + return CirruNode(kind: cirruList, list: buffer, line: expr.line, column: expr.column) proc resolveComma*(expr: CirruNode): CirruNode = case expr.kind - of cirruString: + of cirruToken: return expr - of cirruSeq: + of cirruList: # try to skip some children var hasList = false for child in expr.list: case child.kind - of cirruString: + of cirruToken: discard - of cirruSeq: + of cirruList: hasList = true break if not hasList: @@ -65,13 +65,13 @@ proc resolveComma*(expr: CirruNode): CirruNode = var buffer: DoublyLinkedList[CirruNode] for child in expr.list: case child.kind - of cirruString: + of cirruToken: buffer.append child - of cirruSeq: - if child.list.head.isNil.not and child.list.head.value.kind == cirruString and child.list.head.value.text == ",": + of cirruList: + if child.list.head.isNil.not and child.list.head.value.kind == cirruToken and child.list.head.value.token == ",": let resolvedChild = resolveComma(child) for x in resolvedChild.list.copyFrom(1): buffer.append resolveComma(x) else: buffer.append resolveComma(child) - return CirruNode(kind: cirruSeq, list: buffer, line: expr.line, column: expr.column) + return CirruNode(kind: cirruList, list: buffer, line: expr.line, column: expr.column) diff --git a/src/cirru_parser/types.nim b/src/cirru_parser/types.nim index 4b4f41b..55f9649 100644 --- a/src/cirru_parser/types.nim +++ b/src/cirru_parser/types.nim @@ -4,21 +4,21 @@ import options type CirruNodeKind* = enum - cirruString, - cirruSeq + cirruToken, + cirruList CirruNode* = object line*: int column*: int case kind*: CirruNodeKind - of cirruString: - text*: string - of cirruSeq: + of cirruToken: + token*: string + of cirruList: list*: DoublyLinkedList[CirruNode] LexNodeKind* = enum lexToken, - lexControl + lexOperator ControlOperator* = enum controlParenOpen, @@ -30,8 +30,8 @@ type line*: int column*: int case kind*: LexNodeKind - of lexToken: text*: string - of lexControl: operator*: ControlOperator + of lexToken: token*: string + of lexOperator: operator*: ControlOperator LexState* = enum lexStateToken, @@ -56,10 +56,10 @@ proc raiseParseExceptionAtNode*(msg: string, node: LexNode) = raiseParseException(msg, node.line, node.column) proc isToken*(x: CirruNode): bool = - x.kind == cirruString + x.kind == cirruToken -proc isSeq*(x: CirruNode): bool = - x.kind == cirruSeq +proc isList*(x: CirruNode): bool = + x.kind == cirruList proc toSeq*[T](xs: DoublyLinkedList[T]): seq[T] = var ys: seq[T] @@ -74,7 +74,7 @@ proc toLinkedList*[T](xs: seq[T]): DoublyLinkedList[T] = return ys proc `[]`*(xs: CirruNode, idx: int): Option[CirruNode] = - if xs.kind == cirruString: + if xs.kind == cirruToken: raise newException(ValueError, "Cannot index on cirru string") var i = 0 @@ -85,8 +85,8 @@ proc `[]`*(xs: CirruNode, idx: int): Option[CirruNode] = return none(CirruNode) proc len*(xs: CirruNode): int = - if xs.kind == cirruString: - return xs.text.len + if xs.kind == cirruToken: + return xs.token.len else: var i = 0 for item in xs.list: @@ -98,7 +98,7 @@ proc cirruNodesEqual(x, y: CirruNode): bool = ## compare if two nodes equal if x.kind == y.kind: if x.isToken(): - return x.text == y.text + return x.token == y.token else: if x.len == y.len: var k = 0 @@ -127,16 +127,16 @@ proc `!=`*(x, y: CirruNode): bool = proc isEmpty*(xs: CirruNode): bool = case xs.kind - of cirruString: + of cirruToken: raise newException(ValueError, "Cannot call isEmpty on text CirruNode") - of cirruSeq: + of cirruList: return xs.list.head.isNil proc first*(xs: CirruNode): Option[CirruNode] = case xs.kind - of cirruString: + of cirruToken: raise newException(ValueError, "Cannot call first on text CirruNode") - of cirruSeq: + of cirruList: if xs.isEmpty: return none(CirruNode) else: @@ -153,9 +153,9 @@ proc copyFrom*[T](xs: DoublyLinkedList[T], n: int): DoublyLinkedList[T] = proc restInLinkedList*(xs: CirruNode): DoublyLinkedList[CirruNode] = case xs.kind - of cirruString: + of cirruToken: raise newException(ValueError, "Cannot call rest on text CirruNode") - of cirruSeq: + of cirruList: if xs.isEmpty: raise newException(ValueError, "Cannot call rest on empty CirruNode") else: @@ -163,13 +163,13 @@ proc restInLinkedList*(xs: CirruNode): DoublyLinkedList[CirruNode] = proc rest*(xs: CirruNode): CirruNode = case xs.kind - of cirruString: + of cirruToken: raise newException(ValueError, "Cannot call rest on text CirruNode") - of cirruSeq: + of cirruList: if xs.isEmpty: raise newException(ValueError, "Cannot call rest on empty CirruNode") else: - return CirruNode(kind: cirruSeq, list: xs.list.copyFrom(1)) + return CirruNode(kind: cirruList, list: xs.list.copyFrom(1)) # TODO n proc lexNodesEqual(xs, ys: seq[LexNode]): bool = @@ -185,11 +185,11 @@ proc lexNodesEqual(xs, ys: seq[LexNode]): bool = if xi.kind != yi.kind: return false case xi.kind: - of lexControl: + of lexOperator: if xi.operator != yi.operator: return false of lexToken: - if xi.text != yi.text: + if xi.token != yi.token: return false return true diff --git a/tests/test_lexer.nim b/tests/test_lexer.nim index 7193c4d..1c2de9c 100644 --- a/tests/test_lexer.nim +++ b/tests/test_lexer.nim @@ -1,5 +1,5 @@ -import lists +# import lists import unittest import cirru_parser/types @@ -9,14 +9,14 @@ import cirru_parser/helpers test "Lex nodes equality": check (@[genLexToken("a")] == @[genLexToken("a")]) check (@[genLexToken("a")] != @[genLexToken("b")]) - check (@[genLexControl(controlIndent)] == @[genLexControl(controlIndent)]) - check (@[genLexToken("a")] != @[genLexControl(controlIndent)]) + check (@[genLexOperator(controlIndent)] == @[genLexOperator(controlIndent)]) + check (@[genLexToken("a")] != @[genLexOperator(controlIndent)]) test "Lex code": check (lexCode("a b").toSeq == @[genLexToken("a"), genLexToken("b")]) check (lexCode("a \"b\"").toSeq == @[genLexToken("a"), genLexToken("b")]) check (lexCode("a \"b c\"").toSeq == @[genLexToken("a"), genLexToken("b c")]) - check (lexCode("a\n b").toSeq == @[genLexToken("a"), genLexControl(controlIndent), genLexToken("b")]) - check (lexCode("a (b c)").toSeq == @[genLexToken("a"), genLexControl(controlParenOpen), - genLexToken("b"), genLexToken("c"), genLexControl(controlParenClose)]) - check (lexCode("a\n \"b\"").toSeq == @[genLexToken("a"), genLexControl(controlIndent), genLexToken("b")]) + check (lexCode("a\n b").toSeq == @[genLexToken("a"), genLexOperator(controlIndent), genLexToken("b")]) + check (lexCode("a (b c)").toSeq == @[genLexToken("a"), genLexOperator(controlParenOpen), + genLexToken("b"), genLexToken("c"), genLexOperator(controlParenClose)]) + check (lexCode("a\n \"b\"").toSeq == @[genLexToken("a"), genLexOperator(controlIndent), genLexToken("b")]) diff --git a/tests/test_parser.nim b/tests/test_parser.nim index d44d976..e646fba 100644 --- a/tests/test_parser.nim +++ b/tests/test_parser.nim @@ -10,50 +10,50 @@ test "Parse parens": var a1 = @[ genLexToken("a"), genLexToken("b"), - genLexControl(controlParenClose), + genLexOperator(controlParenClose), ].toLinkedList let b1 = %* ["a", "b"] - check (CirruNode(kind: cirruSeq, list: digestParsingParens(a1)) == toCirru(b1)) + check (CirruNode(kind: cirruList, list: digestParsingParens(a1)) == toCirru(b1)) var a2 = @[ genLexToken("a"), - genLexControl(controlParenOpen), + genLexOperator(controlParenOpen), genLexToken("b"), - genLexControl(controlParenClose), - genLexControl(controlParenClose), + genLexOperator(controlParenClose), + genLexOperator(controlParenClose), ].toLinkedList let b2 = %* ["a", ["b"]] - check (CirruNode(kind: cirruSeq, list: digestParsingParens(a2)) == toCirru(b2)) + check (CirruNode(kind: cirruList, list: digestParsingParens(a2)) == toCirru(b2)) test "Parse indentation": var a1 = @[ genLexToken("a"), - genLexControl(controlOutdent), + genLexOperator(controlOutdent), ].toLinkedList let b1 = %* ["a"] - check (CirruNode(kind: cirruSeq, list: digestParsingIndentation(a1)) == toCirru(b1)) + check (CirruNode(kind: cirruList, list: digestParsingIndentation(a1)) == toCirru(b1)) var a2 = @[ genLexToken("a"), - genLexControl(controlIndent), + genLexOperator(controlIndent), genLexToken("b"), - genLexControl(controlOutdent), - genLexControl(controlOutdent), + genLexOperator(controlOutdent), + genLexOperator(controlOutdent), ].toLinkedList let b2 = %* ["a", ["b"]] - check (CirruNode(kind: cirruSeq, list: digestParsingIndentation(a2)) == toCirru(b2)) + check (CirruNode(kind: cirruList, list: digestParsingIndentation(a2)) == toCirru(b2)) var a3 = @[ genLexToken("a"), - genLexControl(controlIndent), + genLexOperator(controlIndent), genLexToken("b"), - genLexControl(controlOutdent), - genLexControl(controlIndent), + genLexOperator(controlOutdent), + genLexOperator(controlIndent), genLexToken("c"), - genLexControl(controlOutdent), - genLexControl(controlOutdent), + genLexOperator(controlOutdent), + genLexOperator(controlOutdent), ].toLinkedList let b3 = %* ["a", ["b"], ["c"]] - check (CirruNode(kind: cirruSeq, list: digestParsingIndentation(a3)) == toCirru(b3)) + check (CirruNode(kind: cirruList, list: digestParsingIndentation(a3)) == toCirru(b3)) test "Parse simple program": @@ -67,7 +67,7 @@ test "Parse simple program": check (parseCirru("a\n b\nc") == toCirru(a3)) test "Parse empty program": - check (parseCirru("") == CirruNode(kind: cirruSeq, list: initDoublyLinkedList[CirruNode]())) + check (parseCirru("") == CirruNode(kind: cirruList, list: initDoublyLinkedList[CirruNode]())) test "Converts to JSON": check (toJson(parseCirru("a $ b $ c")) == %* [["a", ["b", ["c"]]]]) \ No newline at end of file diff --git a/tests/test_types.nim b/tests/test_types.nim index 2f3315c..3f7c780 100644 --- a/tests/test_types.nim +++ b/tests/test_types.nim @@ -8,17 +8,17 @@ import cirru_parser/types import cirru_parser/helpers test "nodes comparing": - let a1 = CirruNode(kind: cirruString, text: "a") - let a2 = CirruNode(kind: cirruString, text: "a") + let a1 = CirruNode(kind: cirruToken, token: "a") + let a2 = CirruNode(kind: cirruToken, token: "a") check (a1 == a2) - let a3 = CirruNode(kind: cirruString, text: "b") + let a3 = CirruNode(kind: cirruToken, token: "b") check (a1 != a3) let b0 = initDoublyLinkedList[CirruNode]() - let b1 = CirruNode(kind: cirruSeq, list: b0) - let b2 = CirruNode(kind: cirruSeq, list: b0) + let b1 = CirruNode(kind: cirruList, list: b0) + let b2 = CirruNode(kind: cirruList, list: b0) check (b1 == b2) check (a1 != b1) @@ -26,17 +26,17 @@ test "nodes comparing": test "Cirru from JSON": let jsonNodeOfC = JsonNode(kind: JString, str: "c") - let nodeOfC = CirruNode(kind: cirruString, text: "c") + let nodeOfC = CirruNode(kind: cirruToken, token: "c") check (toCirru(jsonNodeOfC) == nodeOfC) let jsonEmpty = %* [] - let nodeOfEmpty = CirruNode(kind: cirruSeq, list: initDoublyLinkedList[CirruNode]()) + let nodeOfEmpty = CirruNode(kind: cirruList, list: initDoublyLinkedList[CirruNode]()) check (toCirru(jsonEmpty) == nodeOfEmpty) let jsonArray = %* ["a"] - let nodeOfArray = CirruNode(kind: cirruSeq, list: @[CirruNode(kind: cirruString, text: "a")].toLinkedList) + let nodeOfArray = CirruNode(kind: cirruList, list: @[CirruNode(kind: cirruToken, token: "a")].toLinkedList) check (toCirru(jsonArray) == nodeOfArray) let jsonNested = %* [[]] - let nodeOfNested = CirruNode(kind: cirruSeq, list: @[CirruNode(kind: cirruSeq, list: initDoublyLinkedList[CirruNode]())].toLinkedList) + let nodeOfNested = CirruNode(kind: cirruList, list: @[CirruNode(kind: cirruList, list: initDoublyLinkedList[CirruNode]())].toLinkedList) check (toCirru(jsonNested) == nodeOfNested)