From 7b0e85a98d6391b804c39633e3d6fe24b28de2bb Mon Sep 17 00:00:00 2001 From: kntkymt Date: Tue, 9 Jan 2024 01:52:48 +0900 Subject: [PATCH] Fix all nodes have TokenNode instead of Token --- Sources/Generator/Generator.swift | 99 ++++---- Sources/Parser/Node/ExpressionNode.swift | 97 +++----- Sources/Parser/Node/Node.swift | 8 + Sources/Parser/Node/PrimaryNode.swift | 91 ++++--- Sources/Parser/Node/StatementNode.swift | 255 +++++++------------- Sources/Parser/Node/TypeNode.swift | 52 ++-- Sources/Parser/Parser.swift | 168 ++++++------- Sources/Tokenizer/Token.swift | 10 +- Sources/Tokenizer/Tokenizer.swift | 6 +- Tests/ParserTest/AssignTest.swift | 28 +-- Tests/ParserTest/BuildTokenTest.swift | 2 +- Tests/ParserTest/Control/ForTest.swift | 58 ++--- Tests/ParserTest/Control/IfTest.swift | 32 +-- Tests/ParserTest/Control/WhileTest.swift | 12 +- Tests/ParserTest/FunctionTest.swift | 134 +++++----- Tests/ParserTest/OperatorPriorityTest.swift | 52 ++-- Tests/ParserTest/OperatorsTest.swift | 116 ++++----- Tests/ParserTest/ReturnTest.swift | 6 +- Tests/ParserTest/StringLiteralTest.swift | 12 +- Tests/ParserTest/VariableTest.swift | 110 ++++----- Tests/TokenizerTest/StringLiteralTest.swift | 3 +- 21 files changed, 644 insertions(+), 707 deletions(-) diff --git a/Sources/Generator/Generator.swift b/Sources/Generator/Generator.swift index b314e89..c406c59 100644 --- a/Sources/Generator/Generator.swift +++ b/Sources/Generator/Generator.swift @@ -79,32 +79,34 @@ public final class Generator { } func generateGlobalVariableDecl(node: VariableDeclNode) throws -> String { - globalVariables[node.identifierName] = node.type + globalVariables[node.identifier.text] = node.type var result = "" if let initializerExpr = node.initializerExpr { - result += ".globl \(node.identifierName)\n" + result += ".globl \(node.identifier.text)\n" if node.type.memorySize != 1 { result += ".p2align 3, 0x0\n" } - result += "\(node.identifierName):\n" + result += "\(node.identifier.text):\n" switch initializerExpr.kind { case .integerLiteral: let sizeKind = node.type.memorySize == 1 ? "byte" : "quad" - let value = try initializerExpr.casted(IntegerLiteralNode.self).literal + let value = try initializerExpr.casted(IntegerLiteralNode.self).literal.text result += " .\(sizeKind) \(value)\n" case .stringLiteral: - let value = try initializerExpr.casted(StringLiteralNode.self).value - result += " .asciz \"\(value)\"\n" + guard case .stringLiteral(let content) = try initializerExpr.casted(StringLiteralNode.self).literal.tokenKind else { + throw GenerateError.invalidSyntax(location: initializerExpr.sourceTokens.first!.sourceRange.start) + } + result += " .asciz \"\(content)\"\n" case .prefixOperatorExpr: let prefixOperatorExpr = try initializerExpr.casted(PrefixOperatorExpressionNode.self) if prefixOperatorExpr.operatorKind == .address { - let value = try prefixOperatorExpr.expression.casted(IdentifierNode.self).identifierName + let value = try prefixOperatorExpr.expression.casted(IdentifierNode.self).baseName.text result += " .quad \(value)\n" } else { throw GenerateError.invalidSyntax(location: initializerExpr.sourceTokens.first!.sourceRange.start) @@ -120,7 +122,7 @@ public final class Generator { let binaryOperator = infixOperator.operator as? BinaryOperatorNode, (binaryOperator.operatorKind == .add || binaryOperator.operatorKind == .sub), let integerLiteral = integerLiteralNode as? IntegerLiteralNode { - return " .quad \(identifier.identifierName) \(binaryOperator.token.value) \(integerLiteral.literal)\n" + return " .quad \(identifier.baseName.text) \(binaryOperator.operator.text) \(integerLiteral.literal.text)\n" } else { return nil } @@ -140,8 +142,8 @@ public final class Generator { } } else { // Apple Clangでは初期化がない場合は.commじゃないとダメっぽい? - let alignment = isElementOrReferenceTypeMemorySizeOf(1, identifierName: node.identifierName) ? 0 : 3 - result += ".comm \(node.identifierName),\(node.type.memorySize),\(alignment)\n" + let alignment = isElementOrReferenceTypeMemorySizeOf(1, identifierName: node.identifier.text) ? 0 : 3 + result += ".comm \(node.identifier.text),\(node.type.memorySize),\(alignment)\n" } return result @@ -153,7 +155,7 @@ public final class Generator { switch node.kind { case .integerLiteral: let casted = try node.casted(IntegerLiteralNode.self) - result += " mov x0, #\(casted.literal)\n" + result += " mov x0, #\(casted.literal.text)\n" result += " str x0, [sp, #-16]!\n" return result @@ -162,12 +164,15 @@ public final class Generator { // stringLiteral自体はグローバル領域に定義し // 式自体は先頭のポインタを表す let casted = try node.casted(StringLiteralNode.self) + guard case .stringLiteral(let content) = casted.literal.tokenKind else { + throw GenerateError.invalidSyntax(location: casted.sourceTokens.first!.sourceRange.start) + } let label: String - if let stringLabel = stringLiteralLabels[casted.value] { + if let stringLabel = stringLiteralLabels[content] { label = stringLabel } else { let stringLabel = "strings\(stringLiteralLabels.count)" - stringLiteralLabels[casted.value] = stringLabel + stringLiteralLabels[content] = stringLabel label = stringLabel } @@ -186,7 +191,7 @@ public final class Generator { // 配列の場合は先頭アドレスのまま返す // 配列以外の場合はアドレスの中身を返す - if let variableType = getVariableType(name: casted.identifierName), variableType.kind != .arrayType { + if let variableType = getVariableType(name: casted.baseName.text), variableType.kind != .arrayType { // アドレスをpop result += " ldr x0, [sp]\n" result += " add sp, sp, #16\n" @@ -218,7 +223,7 @@ public final class Generator { result += " add sp, sp, #16\n" } - let functionLabel = casted.functionName == "main" ? "_main" : casted.functionName + let functionLabel = casted.identifier.text == "main" ? "_main" : casted.identifier.text result += " bl \(functionLabel)\n" // 帰り値をpush @@ -236,7 +241,7 @@ public final class Generator { // 関数定義ごとに作り直す variables = [:] - let functionLabel = casted.functionName == "main" ? "_main" : casted.functionName + let functionLabel = casted.functionName.text == "main" ? "_main" : casted.functionName.text functionLabels.insert(functionLabel) result += "\(functionLabel):\n" @@ -249,9 +254,9 @@ public final class Generator { var parameterDecl = "" // 引数をローカル変数として保存し直す - for (index, parameter) in casted.parameterNodes.enumerated() { + for (index, parameter) in casted.parameters.enumerated() { let offset = (variables.count + 1) * 8 - variables[parameter.identifierName] = VariableInfo(type: parameter.type, addressOffset: offset) + variables[parameter.identifier.text] = VariableInfo(type: parameter.type, addressOffset: offset) parameterDecl += " str x\(index), [x29, #-\(offset)]\n" } @@ -280,7 +285,7 @@ public final class Generator { let casted = try node.casted(VariableDeclNode.self) let offset = variables.reduce(0) { $0 + $1.value.type.memorySize } + casted.type.memorySize - variables[casted.identifierName] = VariableInfo(type: casted.type, addressOffset: offset) + variables[casted.identifier.text] = VariableInfo(type: casted.type, addressOffset: offset) if let initializerExpr = casted.initializerExpr { switch initializerExpr.kind { @@ -289,7 +294,7 @@ public final class Generator { let arrayExpr = try initializerExpr.casted(ArrayExpressionNode.self) for (arrayIndex, element) in arrayExpr.exprListNodes.enumerated() { - result += try generatePushArrayElementAddress(identifierName: casted.identifierName, index: arrayIndex, sourceLocation: casted.identifierToken.sourceRange.start) + result += try generatePushArrayElementAddress(identifierName: casted.identifier.text, index: arrayIndex, sourceLocation: casted.identifier.token.sourceRange.start) result += try generate(node: element) result += " ldr x0, [sp]\n" @@ -297,7 +302,7 @@ public final class Generator { result += " ldr x1, [sp]\n" result += " add sp, sp, #16\n" - if isElementOrReferenceTypeMemorySizeOf(1, identifierName: casted.identifierName) { + if isElementOrReferenceTypeMemorySizeOf(1, identifierName: casted.identifier.text) { result += " strb w0, [x1]\n" } else { result += " str x0, [x1]\n" @@ -306,15 +311,15 @@ public final class Generator { // { ... }の要素が足りなかったら0埋めする let arrayType = try casted.type.casted(ArrayTypeNode.self) - if arrayExpr.exprListNodes.count < arrayType.arraySize { - for arrayIndex in arrayExpr.exprListNodes.count.. String { - try generatePushVariableAddress(identifierName: node.identifierName, sourceLocation: node.token.sourceRange.start) + try generatePushVariableAddress(identifierName: node.baseName.text, sourceLocation: node.baseName.token.sourceRange.start) } private func generatePushVariableAddress(identifierName: String, sourceLocation: SourceLocation) throws -> String { @@ -757,15 +768,15 @@ public final class Generator { var result = "" // 配列の先頭アドレス, subscriptの値をpush - result += try generatePushVariableAddress(node: node.identifierNode) + result += try generatePushVariableAddress(node: node.identifier) result += try generate(node: node.argument) result += " ldr x0, [sp]\n" result += " add sp, sp, #16\n" // subscript内の値は要素のメモリサイズに応じてn倍する - if let arrayType = getVariableType(name: node.identifierNode.identifierName) as? ArrayTypeNode, arrayType.elementType.memorySize == 8 { + if let arrayType = getVariableType(name: node.identifier.baseName.text) as? ArrayTypeNode, arrayType.elementType.memorySize == 8 { result += " lsl x0, x0, 3\n" - } else if let pointerType = getVariableType(name: node.identifierNode.identifierName) as? PointerTypeNode, pointerType.referenceType.memorySize == 8 { + } else if let pointerType = getVariableType(name: node.identifier.baseName.text) as? PointerTypeNode, pointerType.referenceType.memorySize == 8 { result += " lsl x0, x0, 3\n" } @@ -773,7 +784,7 @@ public final class Generator { result += " add sp, sp, #16\n" // identifierがポインタだった場合はアドレスが指す値にする - if variables[node.identifierNode.identifierName]?.type.kind == .pointerType { + if variables[node.identifier.baseName.text]?.type.kind == .pointerType { result += " ldr x1, [x1]\n" } diff --git a/Sources/Parser/Node/ExpressionNode.swift b/Sources/Parser/Node/ExpressionNode.swift index bf6a852..2db0997 100644 --- a/Sources/Parser/Node/ExpressionNode.swift +++ b/Sources/Parser/Node/ExpressionNode.swift @@ -5,9 +5,6 @@ public class InfixOperatorExpressionNode: NodeProtocol { // MARK: - Property public let kind: NodeKind = .infixOperatorExpr - public var sourceTokens: [Token] { - left.sourceTokens + `operator`.sourceTokens + right.sourceTokens - } public var children: [any NodeProtocol] { [left, `operator`, right] } @@ -44,16 +41,15 @@ public class PrefixOperatorExpressionNode: NodeProtocol { // MARK: - Property public let kind: NodeKind = .prefixOperatorExpr - public var sourceTokens: [Token] { - [`operator`] + expression.sourceTokens + public var children: [any NodeProtocol] { + [`operator`, expression] } - public var children: [any NodeProtocol] { [expression] } - public let `operator`: Token + public let `operator`: TokenNode public let expression: any NodeProtocol public var operatorKind: OperatorKind { - switch `operator`.kind { + switch `operator`.tokenKind { case .reserved(.add): return .plus @@ -73,7 +69,7 @@ public class PrefixOperatorExpressionNode: NodeProtocol { // MARK: - Initializer - public init(operator: Token, expression: any NodeProtocol) { + public init(operator: TokenNode, expression: any NodeProtocol) { self.operator = `operator` self.expression = expression } @@ -84,27 +80,22 @@ public class FunctionCallExpressionNode: NodeProtocol { // MARK: - Property public let kind: NodeKind = .functionCallExpr - public var sourceTokens: [Token] { - [identifierToken, parenthesisLeftToken] + arguments.flatMap { $0.sourceTokens } + [parenthesisRightToken] + public var children: [any NodeProtocol] { + [identifier, parenthesisLeft] + arguments + [parenthesisRight] } - public var children: [any NodeProtocol] { arguments } - public let identifierToken: Token - public let parenthesisLeftToken: Token + public let identifier: TokenNode + public let parenthesisLeft: TokenNode public let arguments: [ExpressionListItemNode] - public let parenthesisRightToken: Token - - public var functionName: String { - identifierToken.value - } + public let parenthesisRight: TokenNode // MARK: - Initializer - public init(identifierToken: Token, parenthesisLeftToken: Token, arguments: [ExpressionListItemNode], parenthesisRightToken: Token) { - self.identifierToken = identifierToken - self.parenthesisLeftToken = parenthesisLeftToken + public init(identifier: TokenNode, parenthesisLeft: TokenNode, arguments: [ExpressionListItemNode], parenthesisRight: TokenNode) { + self.identifier = identifier + self.parenthesisLeft = parenthesisLeft self.arguments = arguments - self.parenthesisRightToken = parenthesisRightToken + self.parenthesisRight = parenthesisRight } } @@ -113,21 +104,22 @@ public class SubscriptCallExpressionNode: NodeProtocol { // MARK: - Property public let kind: NodeKind = .subscriptCallExpr - public var sourceTokens: [Token] { identifierNode.sourceTokens + [squareLeftToken] + argument.sourceTokens + [squareRightToken] } - public var children: [any NodeProtocol] { [identifierNode, argument] } + public var children: [any NodeProtocol] { + [identifier, squareLeft, argument, squareRight] + } - public let identifierNode: IdentifierNode - public let squareLeftToken: Token + public let identifier: IdentifierNode + public let squareLeft: TokenNode public let argument: any NodeProtocol - public let squareRightToken: Token + public let squareRight: TokenNode // MARK: - Initializer - public init(identifierNode: IdentifierNode, squareLeftToken: Token, argument: any NodeProtocol, squareRightToken: Token) { - self.identifierNode = identifierNode - self.squareLeftToken = squareLeftToken + public init(identifier: IdentifierNode, squareLeft: TokenNode, argument: any NodeProtocol, squareRight: TokenNode) { + self.identifier = identifier + self.squareLeft = squareLeft self.argument = argument - self.squareRightToken = squareRightToken + self.squareRight = squareRight } } @@ -136,16 +128,17 @@ public class ArrayExpressionNode: NodeProtocol { // MARK: - Property public let kind: NodeKind = .arrayExpr - public var sourceTokens: [Token] { return [braceLeft] + exprListNodes.flatMap { $0.sourceTokens } + [braceRight] } - public var children: [any NodeProtocol] { return exprListNodes.flatMap { $0.children } } + public var children: [any NodeProtocol] { + [braceLeft] + exprListNodes + [braceRight] + } - public let braceLeft: Token + public let braceLeft: TokenNode public let exprListNodes: [ExpressionListItemNode] - public let braceRight: Token + public let braceRight: TokenNode // MARK: - Initializer - public init(braceLeft: Token, exprListNodes: [ExpressionListItemNode], braceRight: Token) { + public init(braceLeft: TokenNode, exprListNodes: [ExpressionListItemNode], braceRight: TokenNode) { self.braceLeft = braceLeft self.exprListNodes = exprListNodes self.braceRight = braceRight @@ -157,25 +150,16 @@ public class ExpressionListItemNode: NodeProtocol { // MARK: - Property public let kind: NodeKind = .exprListItem - public var sourceTokens: [Token] { - var result = expression.sourceTokens - - if let comma { - result += [comma] - } - - return result - } public var children: [any NodeProtocol] { - [expression] + ([expression, comma] as [(any NodeProtocol)?]).compactMap { $0 } } public let expression: any NodeProtocol - public let comma: Token? + public let comma: TokenNode? // MARK: - Initializer - public init(expression: any NodeProtocol, comma: Token? = nil) { + public init(expression: any NodeProtocol, comma: TokenNode? = nil) { self.expression = expression self.comma = comma } @@ -186,22 +170,19 @@ public class TupleExpressionNode: NodeProtocol { // MARK: - Property public let kind: NodeKind = .tupleExpr - public var sourceTokens: [Token] { - [parenthesisLeftToken] + expression.sourceTokens + [parenthesisRightToken] - } public var children: [any NodeProtocol] { - [expression] + [parenthesisLeft, expression, parenthesisRight] } - public let parenthesisLeftToken: Token + public let parenthesisLeft: TokenNode public let expression: any NodeProtocol - public let parenthesisRightToken: Token + public let parenthesisRight: TokenNode // MARK: - Initializer - public init(parenthesisLeftToken: Token, expression: any NodeProtocol, parenthesisRightToken: Token) { - self.parenthesisLeftToken = parenthesisLeftToken + public init(parenthesisLeft: TokenNode, expression: any NodeProtocol, parenthesisRight: TokenNode) { + self.parenthesisLeft = parenthesisLeft self.expression = expression - self.parenthesisRightToken = parenthesisRightToken + self.parenthesisRight = parenthesisRight } } diff --git a/Sources/Parser/Node/Node.swift b/Sources/Parser/Node/Node.swift index 15f90c0..6ed766a 100644 --- a/Sources/Parser/Node/Node.swift +++ b/Sources/Parser/Node/Node.swift @@ -1,6 +1,8 @@ import Tokenizer public enum NodeKind { + case token + case integerLiteral case identifier case stringLiteral @@ -40,6 +42,12 @@ public protocol NodeProtocol: Equatable { var children: [any NodeProtocol] { get } } +public extension NodeProtocol { + var sourceTokens: [Token] { + children.flatMap { $0.sourceTokens } + } +} + extension NodeProtocol { public static func == (lhs: Self, rhs: Self) -> Bool { AnyNode(lhs) == AnyNode(rhs) diff --git a/Sources/Parser/Node/PrimaryNode.swift b/Sources/Parser/Node/PrimaryNode.swift index b6fd1d1..c25a3a4 100644 --- a/Sources/Parser/Node/PrimaryNode.swift +++ b/Sources/Parser/Node/PrimaryNode.swift @@ -1,19 +1,25 @@ import Tokenizer -public class IntegerLiteralNode: NodeProtocol { +public class TokenNode: NodeProtocol { // MARK: - Property - public let kind: NodeKind = .integerLiteral - + public let kind: NodeKind = .token public var sourceTokens: [Token] { [token] } public let children: [any NodeProtocol] = [] + public let token: Token - public var literal: String { - token.value + public var tokenKind: TokenKind { + token.kind + } + public var text: String { + token.text + } + public var description: String { + token.description } // MARK: - Initializer @@ -23,26 +29,39 @@ public class IntegerLiteralNode: NodeProtocol { } } -public class StringLiteralNode: NodeProtocol { +public class IntegerLiteralNode: NodeProtocol { // MARK: - Property - public let kind: NodeKind = .stringLiteral - public var sourceTokens: [Token] { - [token] + public let kind: NodeKind = .integerLiteral + public var children: [any NodeProtocol] { + [literal] } - public let children: [any NodeProtocol] = [] - public let token: Token + public let literal: TokenNode - public var value: String { - token.value + // MARK: - Initializer + + public init(literal: TokenNode) { + self.literal = literal + } +} + +public class StringLiteralNode: NodeProtocol { + + // MARK: - Property + + public let kind: NodeKind = .stringLiteral + public var children: [any NodeProtocol] { + [literal] } + public let literal: TokenNode + // MARK: - Initializer - public init(token: Token) { - self.token = token + public init(literal: TokenNode) { + self.literal = literal } } @@ -51,21 +70,16 @@ public class IdentifierNode: NodeProtocol { // MARK: - Property public let kind: NodeKind = .identifier - - public var sourceTokens: [Token] { - [token] + public var children: [any NodeProtocol] { + [baseName] } - public let children: [any NodeProtocol] = [] - public let token: Token - public var identifierName: String { - token.value - } + public let baseName: TokenNode // MARK: - Initializer - public init(token: Token) { - self.token = token + public init(baseName: TokenNode) { + self.baseName = baseName } } @@ -108,7 +122,7 @@ public class BinaryOperatorNode: NodeProtocol { public let kind: NodeKind = .binaryOperator public var operatorKind: OperatorKind { - switch token.kind { + switch `operator`.tokenKind { case .reserved(.add): return .add @@ -143,16 +157,16 @@ public class BinaryOperatorNode: NodeProtocol { fatalError() } } - public var sourceTokens: [Token] { - [token] + public var children: [any NodeProtocol] { + [`operator`] } - public let children: [any NodeProtocol] = [] - public let token: Token + + public let `operator`: TokenNode // MARK: - Initializer - public init(token: Token) { - self.token = token + public init(operator: TokenNode) { + self.operator = `operator` } } @@ -161,16 +175,15 @@ public class AssignNode: NodeProtocol { // MARK: - Property public let kind: NodeKind = .assign - - public var sourceTokens: [Token] { - [token] + public var children: [any NodeProtocol] { + [equal] } - public let children: [any NodeProtocol] = [] - public let token: Token + + public let equal: TokenNode // MARK: - Initializer - public init(token: Token) { - self.token = token + public init(equal: TokenNode) { + self.equal = equal } } diff --git a/Sources/Parser/Node/StatementNode.swift b/Sources/Parser/Node/StatementNode.swift index 4831a05..eb4ffaf 100644 --- a/Sources/Parser/Node/StatementNode.swift +++ b/Sources/Parser/Node/StatementNode.swift @@ -5,24 +5,23 @@ public class WhileStatementNode: NodeProtocol { // MARK: - Property public let kind: NodeKind = .whileStatement - public var sourceTokens: [Token] { - [whileToken, parenthesisLeftToken] + condition.sourceTokens + [parenthesisRightToken] + body.sourceTokens + public var children: [any NodeProtocol] { + [`while`, parenthesisLeft, condition, parenthesisRight, body] } - public var children: [any NodeProtocol] { [condition, body] } - public let whileToken: Token - public let parenthesisLeftToken: Token + public let `while`: TokenNode + public let parenthesisLeft: TokenNode public let condition: any NodeProtocol - public let parenthesisRightToken: Token + public let parenthesisRight: TokenNode public let body: BlockItemNode // MARK: - Initializer - public init(whileToken: Token, parenthesisLeftToken: Token, condition: any NodeProtocol, parenthesisRightToken: Token, body: BlockItemNode) { - self.whileToken = whileToken - self.parenthesisLeftToken = parenthesisLeftToken + public init(while: TokenNode, parenthesisLeft: TokenNode, condition: any NodeProtocol, parenthesisRight: TokenNode, body: BlockItemNode) { + self.while = `while` + self.parenthesisLeft = parenthesisLeft self.condition = condition - self.parenthesisRightToken = parenthesisRightToken + self.parenthesisRight = parenthesisRight self.body = body } } @@ -32,66 +31,41 @@ public class ForStatementNode: NodeProtocol { // MARK: - Property public let kind: NodeKind = .forStatement - public var sourceTokens: [Token] { - var result = [forToken, parenthesisLeftToken] - - if let pre { - result += pre.sourceTokens - } - - result += [firstSemicolonToken] - - if let condition { - result += condition.sourceTokens - } - - result += [secondSemicolonToken] - - if let post { - result += post.sourceTokens - } - - result += [parenthesisRightToken] - - result += body.sourceTokens - - return result - } public var children: [any NodeProtocol] { - [pre, condition, post, body].compactMap { $0 } + [`for`, parenthesisLeft, pre, firstSemicolon, condition, secondSemicolon, post, parenthesisRight, body].compactMap { $0 } } - public let forToken: Token - public let parenthesisLeftToken: Token + public let `for`: TokenNode + public let parenthesisLeft: TokenNode public let pre: (any NodeProtocol)? - public let firstSemicolonToken: Token + public let firstSemicolon: TokenNode public let condition: (any NodeProtocol)? - public let secondSemicolonToken: Token + public let secondSemicolon: TokenNode public let post: (any NodeProtocol)? - public let parenthesisRightToken: Token + public let parenthesisRight: TokenNode public let body: BlockItemNode // MARK: - Initializer public init( - forToken: Token, - parenthesisLeftToken: Token, + for: TokenNode, + parenthesisLeft: TokenNode, pre: (any NodeProtocol)?, - firstSemicolonToken: Token, + firstSemicolon: TokenNode, condition: (any NodeProtocol)?, - secondSemicolonToken: Token, + secondSemicolon: TokenNode, post: (any NodeProtocol)?, - parenthesisRightToken: Token, + parenthesisRight: TokenNode, body: BlockItemNode ) { - self.forToken = forToken - self.parenthesisLeftToken = parenthesisLeftToken + self.for = `for` + self.parenthesisLeft = parenthesisLeft self.condition = condition - self.firstSemicolonToken = firstSemicolonToken + self.firstSemicolon = firstSemicolon self.pre = pre - self.secondSemicolonToken = secondSemicolonToken + self.secondSemicolon = secondSemicolon self.post = post - self.parenthesisRightToken = parenthesisRightToken + self.parenthesisRight = parenthesisRight self.body = body } } @@ -101,42 +75,33 @@ public class IfStatementNode: NodeProtocol { // MARK: - Property public let kind: NodeKind = .ifStatement - public var sourceTokens: [Token] { - var result = [ifToken, parenthesisLeftToken] + condition.sourceTokens + [parenthesisRightToken] + trueBody.sourceTokens - - if let elseToken { - result.append(elseToken) - } - - return result + (falseBody?.sourceTokens ?? []) - } public var children: [any NodeProtocol] { - [condition, trueBody, falseBody as (any NodeProtocol)?].compactMap { $0 } + ([`if`, parenthesisLeft, condition, parenthesisRight, trueBody, `else`, falseBody] as [(any NodeProtocol)?]).compactMap { $0 } } - public let ifToken: Token - public let parenthesisLeftToken: Token + public let `if`: TokenNode + public let parenthesisLeft: TokenNode public let condition: any NodeProtocol - public let parenthesisRightToken: Token + public let parenthesisRight: TokenNode public let trueBody: BlockItemNode - public let elseToken: Token? + public let `else`: TokenNode? public let falseBody: BlockItemNode? public init( - ifToken: Token, - parenthesisLeftToken: Token, + if: TokenNode, + parenthesisLeft: TokenNode, condition: any NodeProtocol, - parenthesisRightToken: Token, + parenthesisRight: TokenNode, trueBody: BlockItemNode, - elseToken: Token?, + else: TokenNode?, falseBody: BlockItemNode? ) { - self.ifToken = ifToken - self.parenthesisLeftToken = parenthesisLeftToken + self.if = `if` + self.parenthesisLeft = parenthesisLeft self.condition = condition - self.parenthesisRightToken = parenthesisRightToken + self.parenthesisRight = parenthesisRight self.trueBody = trueBody - self.elseToken = elseToken + self.else = `else` self.falseBody = falseBody } } @@ -146,16 +111,17 @@ public class ReturnStatementNode: NodeProtocol { // MARK: - Property public let kind: NodeKind = .returnStatement - public var sourceTokens: [Token] { [returnToken] + expression.sourceTokens } - public var children: [any NodeProtocol] { [expression] } + public var children: [any NodeProtocol] { + [`return`, expression] + } - public let returnToken: Token + public let `return`: TokenNode public let expression: any NodeProtocol // MARK: - Initializer - public init(returnToken: Token, expression: any NodeProtocol) { - self.returnToken = returnToken + public init(return: TokenNode, expression: any NodeProtocol) { + self.return = `return` self.expression = expression } } @@ -165,21 +131,20 @@ public class BlockStatementNode: NodeProtocol { // MARK: - Property public let kind: NodeKind = .blockStatement - public var sourceTokens: [Token] { - [braceLeftToken] + items.flatMap { $0.sourceTokens } + [braceRightToken] + public var children: [any NodeProtocol] { + [braceLeft] + items + [braceRight] } - public var children: [any NodeProtocol] { items } - public let braceLeftToken: Token + public let braceLeft: TokenNode public let items: [BlockItemNode] - public let braceRightToken: Token + public let braceRight: TokenNode // MARK: - Initializer - public init(braceLeftToken: Token, items: [BlockItemNode], braceRightToken: Token) { - self.braceLeftToken = braceLeftToken + public init(braceLeft: TokenNode, items: [BlockItemNode], braceRight: TokenNode) { + self.braceLeft = braceLeft self.items = items - self.braceRightToken = braceRightToken + self.braceRight = braceRight } } @@ -188,27 +153,18 @@ public class BlockItemNode: NodeProtocol { // MARK: - Property public let kind: NodeKind = .blockItem - public var sourceTokens: [Token] { - var result = item.sourceTokens - - if let semicolonToken { - result += [semicolonToken] - } - - return result - } public var children: [any NodeProtocol] { - [item] + ([item, semicolon] as [(any NodeProtocol)?]).compactMap { $0 } } public let item: any NodeProtocol - public let semicolonToken: Token? + public let semicolon: TokenNode? // MARK: - Initializer - public init(item: any NodeProtocol, semicolonToken: Token? = nil) { + public init(item: any NodeProtocol, semicolon: TokenNode? = nil) { self.item = item - self.semicolonToken = semicolonToken + self.semicolon = semicolon } } @@ -217,41 +173,32 @@ public class FunctionDeclNode: NodeProtocol { // MARK: - Property public let kind: NodeKind = .functionDecl - public var sourceTokens: [Token] { - returnTypeNode.sourceTokens - + [functionNameToken, parenthesisLeftToken] - + parameterNodes.flatMap { $0.sourceTokens } - + [parenthesisRightToken] - + block.sourceTokens + public var children: [any NodeProtocol] { + [returnType, functionName, parenthesisLeft] + parameters + [parenthesisRight, block] } - public var children: [any NodeProtocol] { [returnTypeNode] + parameterNodes + [block] } - public let returnTypeNode: any NodeProtocol - public let functionNameToken: Token - public let parenthesisLeftToken: Token - public let parameterNodes: [FunctionParameterNode] - public let parenthesisRightToken: Token + public let returnType: any NodeProtocol + public let functionName: TokenNode + public let parenthesisLeft: TokenNode + public let parameters: [FunctionParameterNode] + public let parenthesisRight: TokenNode public let block: BlockStatementNode - public var functionName: String { - functionNameToken.value - } - // MARK: - Initializer public init( - returnTypeNode: any NodeProtocol, - functionNameToken: Token, - parenthesisLeftToken: Token, - parameterNodes: [FunctionParameterNode], - parenthesisRightToken: Token, + returnType: any NodeProtocol, + functionName: TokenNode, + parenthesisLeft: TokenNode, + parameters: [FunctionParameterNode], + parenthesisRight: TokenNode, block: BlockStatementNode ) { - self.returnTypeNode = returnTypeNode - self.functionNameToken = functionNameToken - self.parenthesisLeftToken = parenthesisLeftToken - self.parameterNodes = parameterNodes - self.parenthesisRightToken = parenthesisRightToken + self.returnType = returnType + self.functionName = functionName + self.parenthesisLeft = parenthesisLeft + self.parameters = parameters + self.parenthesisRight = parenthesisRight self.block = block } } @@ -261,38 +208,21 @@ public class VariableDeclNode: NodeProtocol { // MARK: - Property public let kind: NodeKind = .variableDecl - public var sourceTokens: [Token] { - var tokens = type.sourceTokens + [identifierToken] - - if let initializerToken { - tokens += [initializerToken] - } - - if let initializerExpr { - tokens += initializerExpr.sourceTokens - } - - return tokens - } public var children: [any NodeProtocol] { - [type, initializerExpr].compactMap { $0 } + [type, identifier, equal, initializerExpr].compactMap { $0 } } public let type: any TypeNodeProtocol - public let identifierToken: Token - public let initializerToken: Token? + public let identifier: TokenNode + public let equal: TokenNode? public let initializerExpr: (any NodeProtocol)? - public var identifierName: String { - identifierToken.value - } - // MARK: - Initializer - public init(type: any TypeNodeProtocol, identifierToken: Token, initializerToken: Token? = nil, initializerExpr: (any NodeProtocol)? = nil) { + public init(type: any TypeNodeProtocol, identifier: TokenNode, equal: TokenNode? = nil, initializerExpr: (any NodeProtocol)? = nil) { self.type = type - self.identifierToken = identifierToken - self.initializerToken = initializerToken + self.identifier = identifier + self.equal = equal self.initializerExpr = initializerExpr } } @@ -302,31 +232,20 @@ public class FunctionParameterNode: NodeProtocol { // MARK: - Property public let kind: NodeKind = .functionParameter - public var sourceTokens: [Token] { - var result = type.sourceTokens + [identifierToken] - - if let commaToken { - result += [commaToken] - } - - return result + public var children: [any NodeProtocol] { + ([type, identifier, comma] as [(any NodeProtocol)?]).compactMap { $0 } } - public var children: [any NodeProtocol] { [type] } public let type: any TypeNodeProtocol - public let identifierToken: Token - public let commaToken: Token? - - public var identifierName: String { - identifierToken.value - } + public let identifier: TokenNode + public let comma: TokenNode? // MARK: - Initializer - public init(type: any TypeNodeProtocol, identifierToken: Token, commaToken: Token? = nil) { + public init(type: any TypeNodeProtocol, identifier: TokenNode, comma: TokenNode? = nil) { self.type = type - self.identifierToken = identifierToken - self.commaToken = commaToken + self.identifier = identifier + self.comma = comma } } @@ -335,10 +254,6 @@ public class SourceFileNode: NodeProtocol { // MARK: - Property public let kind: NodeKind = .sourceFile - public var sourceTokens: [Token] { - statements.flatMap { $0.sourceTokens } - } - public var children: [any NodeProtocol] { statements } diff --git a/Sources/Parser/Node/TypeNode.swift b/Sources/Parser/Node/TypeNode.swift index b8ec4e0..4718c6d 100644 --- a/Sources/Parser/Node/TypeNode.swift +++ b/Sources/Parser/Node/TypeNode.swift @@ -10,13 +10,14 @@ public class TypeNode: TypeNodeProtocol { // MARK: - Property public let kind: NodeKind = .type - public var sourceTokens: [Token] { [typeToken] } - public let children: [any NodeProtocol] = [] + public var children: [any NodeProtocol] { + [type] + } - public let typeToken: Token + public let type: TokenNode public var memorySize: Int { - return switch typeToken.kind { + return switch type.tokenKind { case .type(let typeKind): switch typeKind { case .int: 8 @@ -30,8 +31,8 @@ public class TypeNode: TypeNodeProtocol { // MARK: - Initializer - public init(typeToken: Token) { - self.typeToken = typeToken + public init(type: TokenNode) { + self.type = type } } @@ -40,19 +41,20 @@ public class PointerTypeNode: TypeNodeProtocol { // MARK: - Property public let kind: NodeKind = .pointerType - public var sourceTokens: [Token] { referenceType.sourceTokens + [pointerToken] } - public var children: [any NodeProtocol] { [referenceType] } - public let referenceType: any TypeNodeProtocol + public var children: [any NodeProtocol] { + [referenceType, pointer] + } - public let pointerToken: Token + public let referenceType: any TypeNodeProtocol + public let pointer: TokenNode public let memorySize: Int = 8 // MARK: - Initializer - public init(referenceType: any TypeNodeProtocol, pointerToken: Token) { + public init(referenceType: any TypeNodeProtocol, pointer: TokenNode) { self.referenceType = referenceType - self.pointerToken = pointerToken + self.pointer = pointer } } @@ -61,28 +63,28 @@ public class ArrayTypeNode: TypeNodeProtocol { // MARK: - Property public let kind: NodeKind = .arrayType + public var children: [any NodeProtocol] { + [elementType, squareLeft, arraySize, squareRight] + } - public var sourceTokens: [Token] { [squareLeftToken] + elementType.sourceTokens + [squareRightToken] } - public var children: [any NodeProtocol] { [elementType] } public let elementType: any TypeNodeProtocol + public let squareLeft: TokenNode + public let arraySize: TokenNode + public let squareRight: TokenNode - public let squareLeftToken: Token - public let arraySizeToken: Token - public let squareRightToken: Token - - public var arraySize: Int { - Int(arraySizeToken.value)! + public var arrayLength: Int { + Int(arraySize.text)! } public var memorySize: Int { - arraySize * elementType.memorySize + arrayLength * elementType.memorySize } // MARK: - Initializer - public init(elementType: any TypeNodeProtocol, squareLeftToken: Token, arraySizeToken: Token, squareRightToken: Token) { + public init(elementType: any TypeNodeProtocol, squareLeft: TokenNode, arraySize: TokenNode, squareRight: TokenNode) { self.elementType = elementType - self.squareLeftToken = squareLeftToken - self.arraySizeToken = arraySizeToken - self.squareRightToken = squareRightToken + self.squareLeft = squareLeft + self.arraySize = arraySize + self.squareRight = squareRight } } diff --git a/Sources/Parser/Parser.swift b/Sources/Parser/Parser.swift index 155d71d..e2e9204 100644 --- a/Sources/Parser/Parser.swift +++ b/Sources/Parser/Parser.swift @@ -18,7 +18,7 @@ public final class Parser { // MARK: - Util @discardableResult - func consumeIdentifierToken() throws -> Token { + func consumeIdentifierToken() throws -> TokenNode { if index >= tokens.count { throw ParseError.invalidSyntax(location: tokens.last.map { $0.sourceRange.end } ?? .startOfFile) } @@ -26,14 +26,14 @@ public final class Parser { if case .identifier = tokens[index].kind { let token = tokens[index] index += 1 - return token + return TokenNode(token: token) } else { throw ParseError.invalidSyntax(location: tokens[index].sourceRange.start) } } @discardableResult - func consumeNumberToken() throws -> Token { + func consumeNumberToken() throws -> TokenNode { if index >= tokens.count { throw ParseError.invalidSyntax(location: tokens.last.map { $0.sourceRange.end } ?? .startOfFile) } @@ -41,14 +41,14 @@ public final class Parser { if case .number = tokens[index].kind { let token = tokens[index] index += 1 - return token + return TokenNode(token: token) } else { throw ParseError.invalidSyntax(location: tokens[index].sourceRange.start) } } @discardableResult - func consumeStringLiteralToken() throws -> Token { + func consumeStringLiteralToken() throws -> TokenNode { if index >= tokens.count { throw ParseError.invalidSyntax(location: tokens.last.map { $0.sourceRange.end } ?? .startOfFile) } @@ -56,14 +56,14 @@ public final class Parser { if case .stringLiteral = tokens[index].kind { let token = tokens[index] index += 1 - return token + return TokenNode(token: token) } else { throw ParseError.invalidSyntax(location: tokens[index].sourceRange.start) } } @discardableResult - func consumeReservedToken(_ reservedKind: TokenKind.ReservedKind) throws -> Token { + func consumeReservedToken(_ reservedKind: TokenKind.ReservedKind) throws -> TokenNode { if index >= tokens.count { throw ParseError.invalidSyntax(location: tokens.last.map { $0.sourceRange.end } ?? .startOfFile) } @@ -71,14 +71,14 @@ public final class Parser { if case .reserved(let kind) = tokens[index].kind, kind == reservedKind { let token = tokens[index] index += 1 - return token + return TokenNode(token: token) } else { throw ParseError.invalidSyntax(location: tokens[index].sourceRange.start) } } @discardableResult - func consumeKeywordToken(_ keywordKind: TokenKind.KeywordKind) throws -> Token { + func consumeKeywordToken(_ keywordKind: TokenKind.KeywordKind) throws -> TokenNode { if index >= tokens.count { throw ParseError.invalidSyntax(location: tokens.last.map { $0.sourceRange.end } ?? .startOfFile) } @@ -86,14 +86,14 @@ public final class Parser { if case .keyword(let kind) = tokens[index].kind, kind == keywordKind { let token = tokens[index] index += 1 - return token + return TokenNode(token: token) } else { throw ParseError.invalidSyntax(location: tokens[index].sourceRange.start) } } @discardableResult - func consumeTypeToken() throws -> Token { + func consumeTypeToken() throws -> TokenNode { if index >= tokens.count { throw ParseError.invalidSyntax(location: tokens.last.map { $0.sourceRange.end } ?? .startOfFile) } @@ -101,7 +101,7 @@ public final class Parser { if case .type = tokens[index].kind { let token = tokens[index] index += 1 - return token + return TokenNode(token: token) } else { throw ParseError.invalidSyntax(location: tokens[index].sourceRange.start) } @@ -141,17 +141,17 @@ public final class Parser { let parenthesisRight = try consumeReservedToken(.parenthesisRight) let function = FunctionDeclNode( - returnTypeNode: type, - functionNameToken: identifier, - parenthesisLeftToken: parenthesisLeft, - parameterNodes: parameters, - parenthesisRightToken: parenthesisRight, + returnType: type, + functionName: identifier, + parenthesisLeft: parenthesisLeft, + parameters: parameters, + parenthesisRight: parenthesisRight, block: try block() ) statements.append(BlockItemNode(item: function)) } else { let variable = try variableDecl(variableType: type, identifier: identifier) - statements.append(BlockItemNode(item: variable, semicolonToken: try consumeReservedToken(.semicolon))) + statements.append(BlockItemNode(item: variable, semicolon: try consumeReservedToken(.semicolon))) } } @@ -186,22 +186,22 @@ public final class Parser { if index < tokens.count, case .reserved(.squareLeft) = tokens[index].kind { type = ArrayTypeNode( elementType: type, - squareLeftToken: try consumeReservedToken(.squareLeft), - arraySizeToken: try consumeNumberToken(), - squareRightToken: try consumeReservedToken(.squareRight) + squareLeft: try consumeReservedToken(.squareLeft), + arraySize: try consumeNumberToken(), + squareRight: try consumeReservedToken(.squareRight) ) } if index < tokens.count, case .reserved(.comma) = tokens[index].kind { return FunctionParameterNode( type: type, - identifierToken: identifier, - commaToken: try consumeReservedToken(.comma) + identifier: identifier, + comma: try consumeReservedToken(.comma) ) } else { return FunctionParameterNode( type: type, - identifierToken: identifier + identifier: identifier ) } } @@ -222,7 +222,7 @@ public final class Parser { return BlockItemNode(item: try block()) case .type: - return BlockItemNode(item: try variableDecl(), semicolonToken: try consumeReservedToken(.semicolon)) + return BlockItemNode(item: try variableDecl(), semicolon: try consumeReservedToken(.semicolon)) case .keyword(.if): let ifToken = try consumeKeywordToken(.if) @@ -233,7 +233,7 @@ public final class Parser { let trueStatement = try stmt() - var elseToken: Token? + var elseToken: TokenNode? var falseStatement: BlockItemNode? if index < tokens.count, case .keyword(.else) = tokens[index].kind { elseToken = try consumeKeywordToken(.else) @@ -241,12 +241,12 @@ public final class Parser { } let ifStatement = IfStatementNode( - ifToken: ifToken, - parenthesisLeftToken: parenthesisLeft, + if: ifToken, + parenthesisLeft: parenthesisLeft, condition: condition, - parenthesisRightToken: parenthesisRight, + parenthesisRight: parenthesisRight, trueBody: trueStatement, - elseToken: elseToken, + else: elseToken, falseBody: falseStatement ) return BlockItemNode(item: ifStatement) @@ -259,10 +259,10 @@ public final class Parser { let parenthesisRight = try consumeReservedToken(.parenthesisRight) let whileStatement = WhileStatementNode( - whileToken: token, - parenthesisLeftToken: parenthesisLeft, + while: token, + parenthesisLeft: parenthesisLeft, condition: condition, - parenthesisRightToken: parenthesisRight, + parenthesisRight: parenthesisRight, body: try stmt() ) return BlockItemNode(item: whileStatement) @@ -272,7 +272,7 @@ public final class Parser { let parenthesisLeft = try consumeReservedToken(.parenthesisLeft) var preExpr: (any NodeProtocol)? - let firstSemicolon: Token + let firstSemicolon: TokenNode if case .reserved(.semicolon) = tokens[index].kind { firstSemicolon = try consumeReservedToken(.semicolon) } else { @@ -281,7 +281,7 @@ public final class Parser { } var condition: (any NodeProtocol)? - let secondSemicolon: Token + let secondSemicolon: TokenNode if case .reserved(.semicolon) = tokens[index].kind { secondSemicolon = try consumeReservedToken(.semicolon) } else { @@ -290,7 +290,7 @@ public final class Parser { } var postExpr: (any NodeProtocol)? - let parenthesisRight: Token + let parenthesisRight: TokenNode if case .reserved(.parenthesisRight) = tokens[index].kind { parenthesisRight = try consumeReservedToken(.parenthesisRight) } else { @@ -299,14 +299,14 @@ public final class Parser { } let forStatement = ForStatementNode( - forToken: forToken, - parenthesisLeftToken: parenthesisLeft, + for: forToken, + parenthesisLeft: parenthesisLeft, pre: preExpr, - firstSemicolonToken: firstSemicolon, + firstSemicolon: firstSemicolon, condition: condition, - secondSemicolonToken: secondSemicolon, + secondSemicolon: secondSemicolon, post: postExpr, - parenthesisRightToken: parenthesisRight, + parenthesisRight: parenthesisRight, body: try stmt() ) return BlockItemNode(item: forStatement) @@ -315,16 +315,16 @@ public final class Parser { let token = try consumeKeywordToken(.return) let left = try expr() - let returnStatement = ReturnStatementNode(returnToken: token, expression: left) + let returnStatement = ReturnStatementNode(return: token, expression: left) return BlockItemNode( item: returnStatement, - semicolonToken: try consumeReservedToken(.semicolon) + semicolon: try consumeReservedToken(.semicolon) ) default: return BlockItemNode( item: try expr(), - semicolonToken: try consumeReservedToken(.semicolon) + semicolon: try consumeReservedToken(.semicolon) ) } } @@ -345,35 +345,35 @@ public final class Parser { let braceRight = try consumeReservedToken(.braceRight) return BlockStatementNode( - braceLeftToken: braceLeft, + braceLeft: braceLeft, items: items, - braceRightToken: braceRight + braceRight: braceRight ) } // variableDecl = type identifier ("[" num "]")? ("=" (expr | "{" exprList "}" | stringLiteral)? ";" - func variableDecl(variableType: (any TypeNodeProtocol)? = nil, identifier: Token? = nil) throws -> VariableDeclNode { + func variableDecl(variableType: (any TypeNodeProtocol)? = nil, identifier: TokenNode? = nil) throws -> VariableDeclNode { var type = if let variableType { variableType } else { try type() } let identifier = if let identifier { identifier } else { try consumeIdentifierToken() } if index < tokens.count, case .reserved(.squareLeft) = tokens[index].kind { type = ArrayTypeNode( elementType: type, - squareLeftToken: try consumeReservedToken(.squareLeft), - arraySizeToken: try consumeNumberToken(), - squareRightToken: try consumeReservedToken(.squareRight) + squareLeft: try consumeReservedToken(.squareLeft), + arraySize: try consumeNumberToken(), + squareRight: try consumeReservedToken(.squareRight) ) } if index < tokens.count, case .reserved(.assign) = tokens[index].kind { - let initializerToken = try consumeReservedToken(.assign) + let initializer = try consumeReservedToken(.assign) switch tokens[index].kind { case .reserved(.braceLeft): return VariableDeclNode( type: type, - identifierToken: identifier, - initializerToken: initializerToken, + identifier: identifier, + equal: initializer, initializerExpr: ArrayExpressionNode( braceLeft: try consumeReservedToken(.braceLeft), exprListNodes: try exprList(), @@ -384,16 +384,16 @@ public final class Parser { case .stringLiteral: return VariableDeclNode( type: type, - identifierToken: identifier, - initializerToken: initializerToken, - initializerExpr: StringLiteralNode(token: try consumeStringLiteralToken()) + identifier: identifier, + equal: initializer, + initializerExpr: StringLiteralNode(literal: try consumeStringLiteralToken()) ) default: return VariableDeclNode( type: type, - identifierToken: identifier, - initializerToken: initializerToken, + identifier: identifier, + equal: initializer, initializerExpr: try expr() ) } @@ -401,18 +401,18 @@ public final class Parser { return VariableDeclNode( type: type, - identifierToken: identifier + identifier: identifier ) } // type = typeIdentifier "*"* func type() throws -> any TypeNodeProtocol { - var node: any TypeNodeProtocol = TypeNode(typeToken: try consumeTypeToken()) + var node: any TypeNodeProtocol = TypeNode(type: try consumeTypeToken()) while index < tokens.count { if case .reserved(.mul) = tokens[index].kind { let mulToken = try consumeReservedToken(.mul) - node = PointerTypeNode(referenceType: node, pointerToken: mulToken) + node = PointerTypeNode(referenceType: node, pointer: mulToken) } else { break } @@ -440,7 +440,7 @@ public final class Parser { node = InfixOperatorExpressionNode( left: node, - operator: AssignNode(token: token), + operator: AssignNode(equal: token), right: rightNode ) } @@ -460,7 +460,7 @@ public final class Parser { node = InfixOperatorExpressionNode( left: node, - operator: BinaryOperatorNode(token: token), + operator: BinaryOperatorNode(operator: token), right: rightNode ) @@ -470,7 +470,7 @@ public final class Parser { node = InfixOperatorExpressionNode( left: node, - operator: BinaryOperatorNode(token: token), + operator: BinaryOperatorNode(operator: token), right: rightNode ) @@ -494,7 +494,7 @@ public final class Parser { node = InfixOperatorExpressionNode( left: node, - operator: BinaryOperatorNode(token: token), + operator: BinaryOperatorNode(operator: token), right: rightNode ) @@ -504,7 +504,7 @@ public final class Parser { node = InfixOperatorExpressionNode( left: node, - operator: BinaryOperatorNode(token: token), + operator: BinaryOperatorNode(operator: token), right: rightNode ) @@ -514,7 +514,7 @@ public final class Parser { node = InfixOperatorExpressionNode( left: node, - operator: BinaryOperatorNode(token: token), + operator: BinaryOperatorNode(operator: token), right: rightNode ) @@ -524,7 +524,7 @@ public final class Parser { node = InfixOperatorExpressionNode( left: node, - operator: BinaryOperatorNode(token: token), + operator: BinaryOperatorNode(operator: token), right: rightNode ) @@ -548,7 +548,7 @@ public final class Parser { node = InfixOperatorExpressionNode( left: node, - operator: BinaryOperatorNode(token: addToken), + operator: BinaryOperatorNode(operator: addToken), right: rightNode ) @@ -558,7 +558,7 @@ public final class Parser { node = InfixOperatorExpressionNode( left: node, - operator: BinaryOperatorNode(token: subToken), + operator: BinaryOperatorNode(operator: subToken), right: rightNode ) @@ -582,7 +582,7 @@ public final class Parser { node = InfixOperatorExpressionNode( left: node, - operator: BinaryOperatorNode(token: mulToken), + operator: BinaryOperatorNode(operator: mulToken), right: rightNode ) @@ -592,7 +592,7 @@ public final class Parser { node = InfixOperatorExpressionNode( left: node, - operator: BinaryOperatorNode(token: divToken), + operator: BinaryOperatorNode(operator: divToken), right: rightNode ) @@ -619,7 +619,7 @@ public final class Parser { _ = try unary() // FIXME: どうやって式の型を判断する? - return IntegerLiteralNode(token: Token(kind: .number("8"), sourceRange: SourceRange(start: .startOfFile, end: .startOfFile))) + return IntegerLiteralNode(literal: TokenNode(token: Token(kind: .number("8"), sourceRange: SourceRange(start: .startOfFile, end: .startOfFile)))) case .reserved(.add): return PrefixOperatorExpressionNode( @@ -668,20 +668,20 @@ public final class Parser { switch tokens[index].kind { case .reserved(.parenthesisLeft): return TupleExpressionNode( - parenthesisLeftToken: try consumeReservedToken(.parenthesisLeft), + parenthesisLeft: try consumeReservedToken(.parenthesisLeft), expression: try expr(), - parenthesisRightToken: try consumeReservedToken(.parenthesisRight) + parenthesisRight: try consumeReservedToken(.parenthesisRight) ) case .number: let numberToken = try consumeNumberToken() - let numberNode = IntegerLiteralNode(token: numberToken) + let numberNode = IntegerLiteralNode(literal: numberToken) return numberNode case .stringLiteral: let stringToken = try consumeStringLiteralToken() - let stringLiteralNode = StringLiteralNode(token: stringToken) + let stringLiteralNode = StringLiteralNode(literal: stringToken) return stringLiteralNode @@ -703,20 +703,20 @@ public final class Parser { let parenthesisRight = try consumeReservedToken(.parenthesisRight) return FunctionCallExpressionNode( - identifierToken: identifierToken, - parenthesisLeftToken: parenthesisLeft, + identifier: identifierToken, + parenthesisLeft: parenthesisLeft, arguments: argments, - parenthesisRightToken: parenthesisRight + parenthesisRight: parenthesisRight ) } else if index < tokens.count, case .reserved(.squareLeft) = tokens[index].kind { return SubscriptCallExpressionNode( - identifierNode: IdentifierNode(token: identifierToken), - squareLeftToken: try consumeReservedToken(.squareLeft), + identifier: IdentifierNode(baseName: identifierToken), + squareLeft: try consumeReservedToken(.squareLeft), argument: try expr(), - squareRightToken: try consumeReservedToken(.squareRight) + squareRight: try consumeReservedToken(.squareRight) ) } else { - return IdentifierNode(token: identifierToken) + return IdentifierNode(baseName: identifierToken) } default: diff --git a/Sources/Tokenizer/Token.swift b/Sources/Tokenizer/Token.swift index 784a606..3409834 100644 --- a/Sources/Tokenizer/Token.swift +++ b/Sources/Tokenizer/Token.swift @@ -8,13 +8,13 @@ public struct Token: Equatable { public var sourceRange: SourceRange /// without trivia - public var value: String { - kind.value + public var text: String { + kind.text } /// with trivia public var description: String { - leadingTrivia + kind.value + trailingTrivia + leadingTrivia + kind.text + trailingTrivia } // MARK: - Initializer @@ -72,7 +72,7 @@ public enum TokenKind: Equatable { case identifier(_ value: String) case type(_ kind: TypeKind) - public var value: String { + public var text: String { switch self { case .reserved(let kind): return kind.rawValue @@ -84,7 +84,7 @@ public enum TokenKind: Equatable { return value case .stringLiteral(let value): - return value + return "\"" + value + "\"" case .identifier(let value): return value diff --git a/Sources/Tokenizer/Tokenizer.swift b/Sources/Tokenizer/Tokenizer.swift index b762efa..262524d 100644 --- a/Sources/Tokenizer/Tokenizer.swift +++ b/Sources/Tokenizer/Tokenizer.swift @@ -74,7 +74,7 @@ public class Tokenizer { } private func extractString() -> TokenKind { - var string = "" + var content = "" // 開始の" index += 1 @@ -86,12 +86,12 @@ public class Tokenizer { index += 1 break } else { - string += String(nextToken) + content += String(nextToken) index += 1 } } - return .stringLiteral(string) + return .stringLiteral(content) } private func extractIdentifier() -> TokenKind { diff --git a/Tests/ParserTest/AssignTest.swift b/Tests/ParserTest/AssignTest.swift index f651ee9..1624ce4 100644 --- a/Tests/ParserTest/AssignTest.swift +++ b/Tests/ParserTest/AssignTest.swift @@ -21,11 +21,11 @@ final class AssignTest: XCTestCase { node, BlockItemNode( item: InfixOperatorExpressionNode( - left: IdentifierNode(token: tokens[0]), - operator: AssignNode(token: tokens[1]), - right: IntegerLiteralNode(token: tokens[2]) + left: IdentifierNode(baseName: TokenNode(token: tokens[0])), + operator: AssignNode(equal: TokenNode(token: tokens[1])), + right: IntegerLiteralNode(literal: TokenNode(token: tokens[2])) ), - semicolonToken: tokens[3] + semicolon: TokenNode(token: tokens[3]) ) ) } @@ -49,15 +49,15 @@ final class AssignTest: XCTestCase { node, BlockItemNode( item: InfixOperatorExpressionNode( - left: IdentifierNode(token: tokens[0]), - operator: AssignNode(token: tokens[1]), + left: IdentifierNode(baseName: TokenNode(token: tokens[0])), + operator: AssignNode(equal: TokenNode(token: tokens[1])), right: InfixOperatorExpressionNode( - left: IdentifierNode(token: tokens[2]), - operator: AssignNode(token: tokens[3]), - right: IntegerLiteralNode(token: tokens[4]) + left: IdentifierNode(baseName: TokenNode(token: tokens[2])), + operator: AssignNode(equal: TokenNode(token: tokens[3])), + right: IntegerLiteralNode(literal: TokenNode(token: tokens[4])) ) ), - semicolonToken: tokens[5] + semicolon: TokenNode(token: tokens[5]) ) ) } @@ -80,11 +80,11 @@ final class AssignTest: XCTestCase { node, BlockItemNode( item: InfixOperatorExpressionNode( - left: IntegerLiteralNode(token: tokens[0]), - operator: AssignNode(token: tokens[1]), - right: IntegerLiteralNode(token: tokens[2]) + left: IntegerLiteralNode(literal: TokenNode(token: tokens[0])), + operator: AssignNode(equal: TokenNode(token: tokens[1])), + right: IntegerLiteralNode(literal: TokenNode(token: tokens[2])) ), - semicolonToken: tokens[3] + semicolon: TokenNode(token: tokens[3]) ) ) } diff --git a/Tests/ParserTest/BuildTokenTest.swift b/Tests/ParserTest/BuildTokenTest.swift index 2d0c764..c40845f 100644 --- a/Tests/ParserTest/BuildTokenTest.swift +++ b/Tests/ParserTest/BuildTokenTest.swift @@ -7,7 +7,7 @@ func buildTokens(kinds: [TokenKind]) -> [Token] { for kind in kinds { let start = SourceLocation(line: 1, column: column) - column += kind.value.count + column += kind.text.count let end = SourceLocation(line: 1, column: column) let token = Token( diff --git a/Tests/ParserTest/Control/ForTest.swift b/Tests/ParserTest/Control/ForTest.swift index 73d44c0..6e0f447 100644 --- a/Tests/ParserTest/Control/ForTest.swift +++ b/Tests/ParserTest/Control/ForTest.swift @@ -27,17 +27,17 @@ final class ForTest: XCTestCase { node, BlockItemNode( item: ForStatementNode( - forToken: tokens[0], - parenthesisLeftToken: tokens[1], - pre: IntegerLiteralNode(token: tokens[2]), - firstSemicolonToken: tokens[3], - condition: IntegerLiteralNode(token: tokens[4]), - secondSemicolonToken: tokens[5], - post: IntegerLiteralNode(token: tokens[6]), - parenthesisRightToken: tokens[7], + for: TokenNode(token: tokens[0]), + parenthesisLeft: TokenNode(token: tokens[1]), + pre: IntegerLiteralNode(literal: TokenNode(token: tokens[2])), + firstSemicolon: TokenNode(token: tokens[3]), + condition: IntegerLiteralNode(literal: TokenNode(token: tokens[4])), + secondSemicolon: TokenNode(token: tokens[5]), + post: IntegerLiteralNode(literal: TokenNode(token: tokens[6])), + parenthesisRight: TokenNode(token: tokens[7]), body: BlockItemNode( - item: IntegerLiteralNode(token: tokens[8]), - semicolonToken: tokens[9] + item: IntegerLiteralNode(literal: TokenNode(token: tokens[8])), + semicolon: TokenNode(token: tokens[9]) ) ) ) @@ -71,22 +71,22 @@ final class ForTest: XCTestCase { node, BlockItemNode( item: ForStatementNode( - forToken: tokens[0], - parenthesisLeftToken: tokens[1], - pre: IntegerLiteralNode(token: tokens[2]), - firstSemicolonToken: tokens[3], - condition: IntegerLiteralNode(token: tokens[4]), - secondSemicolonToken: tokens[5], - post: IntegerLiteralNode(token: tokens[6]), - parenthesisRightToken: tokens[7], + for: TokenNode(token: tokens[0]), + parenthesisLeft: TokenNode(token: tokens[1]), + pre: IntegerLiteralNode(literal: TokenNode(token: tokens[2])), + firstSemicolon: TokenNode(token: tokens[3]), + condition: IntegerLiteralNode(literal: TokenNode(token: tokens[4])), + secondSemicolon: TokenNode(token: tokens[5]), + post: IntegerLiteralNode(literal: TokenNode(token: tokens[6])), + parenthesisRight: TokenNode(token: tokens[7]), body: BlockItemNode( item: BlockStatementNode( - braceLeftToken: tokens[8], + braceLeft: TokenNode(token: tokens[8]), items: [ - BlockItemNode(item: IntegerLiteralNode(token: tokens[9]), semicolonToken: tokens[10]), - BlockItemNode(item: IntegerLiteralNode(token: tokens[11]), semicolonToken: tokens[12]) + BlockItemNode(item: IntegerLiteralNode(literal: TokenNode(token: tokens[9])), semicolon: TokenNode(token: tokens[10])), + BlockItemNode(item: IntegerLiteralNode(literal: TokenNode(token: tokens[11])), semicolon: TokenNode(token: tokens[12])) ], - braceRightToken: tokens[13] + braceRight: TokenNode(token: tokens[13]) ) ) ) @@ -114,17 +114,17 @@ final class ForTest: XCTestCase { node, BlockItemNode( item: ForStatementNode( - forToken: tokens[0], - parenthesisLeftToken: tokens[1], + for: TokenNode(token: tokens[0]), + parenthesisLeft: TokenNode(token: tokens[1]), pre: nil, - firstSemicolonToken: tokens[2], + firstSemicolon: TokenNode(token: tokens[2]), condition: nil, - secondSemicolonToken: tokens[3], + secondSemicolon: TokenNode(token: tokens[3]), post: nil, - parenthesisRightToken: tokens[4], + parenthesisRight: TokenNode(token: tokens[4]), body: BlockItemNode( - item: IntegerLiteralNode(token: tokens[5]), - semicolonToken: tokens[6] + item: IntegerLiteralNode(literal: TokenNode(token: tokens[5])), + semicolon: TokenNode(token: tokens[6]) ) ) ) diff --git a/Tests/ParserTest/Control/IfTest.swift b/Tests/ParserTest/Control/IfTest.swift index ff7c309..5159e81 100644 --- a/Tests/ParserTest/Control/IfTest.swift +++ b/Tests/ParserTest/Control/IfTest.swift @@ -23,15 +23,15 @@ final class IfTest: XCTestCase { node, BlockItemNode( item: IfStatementNode( - ifToken: tokens[0], - parenthesisLeftToken: tokens[1], - condition: IntegerLiteralNode(token: tokens[2]), - parenthesisRightToken: tokens[3], + if: TokenNode(token: tokens[0]), + parenthesisLeft: TokenNode(token: tokens[1]), + condition: IntegerLiteralNode(literal: TokenNode(token: tokens[2])), + parenthesisRight: TokenNode(token: tokens[3]), trueBody: BlockItemNode( - item: IntegerLiteralNode(token: tokens[4]), - semicolonToken: tokens[5] + item: IntegerLiteralNode(literal: TokenNode(token: tokens[4])), + semicolon: TokenNode(token: tokens[5]) ), - elseToken: nil, + else: nil, falseBody: nil ) ) @@ -60,18 +60,18 @@ final class IfTest: XCTestCase { node, BlockItemNode( item: IfStatementNode( - ifToken: tokens[0], - parenthesisLeftToken: tokens[1], - condition: IntegerLiteralNode(token: tokens[2]), - parenthesisRightToken: tokens[3], + if: TokenNode(token: tokens[0]), + parenthesisLeft: TokenNode(token: tokens[1]), + condition: IntegerLiteralNode(literal: TokenNode(token: tokens[2])), + parenthesisRight: TokenNode(token: tokens[3]), trueBody: BlockItemNode( - item: IntegerLiteralNode(token: tokens[4]), - semicolonToken: tokens[5] + item: IntegerLiteralNode(literal: TokenNode(token: tokens[4])), + semicolon: TokenNode(token: tokens[5]) ), - elseToken: tokens[6], + else: TokenNode(token: tokens[6]), falseBody: BlockItemNode( - item: IntegerLiteralNode(token: tokens[7]), - semicolonToken: tokens[8] + item: IntegerLiteralNode(literal: TokenNode(token: tokens[7])), + semicolon: TokenNode(token: tokens[8]) ) ) ) diff --git a/Tests/ParserTest/Control/WhileTest.swift b/Tests/ParserTest/Control/WhileTest.swift index c7e3518..531a551 100644 --- a/Tests/ParserTest/Control/WhileTest.swift +++ b/Tests/ParserTest/Control/WhileTest.swift @@ -23,13 +23,13 @@ final class WhileTest: XCTestCase { node, BlockItemNode( item: WhileStatementNode( - whileToken: tokens[0], - parenthesisLeftToken: tokens[1], - condition: IntegerLiteralNode(token: tokens[2]), - parenthesisRightToken: tokens[3], + while: TokenNode(token: tokens[0]), + parenthesisLeft: TokenNode(token: tokens[1]), + condition: IntegerLiteralNode(literal: TokenNode(token: tokens[2])), + parenthesisRight: TokenNode(token: tokens[3]), body: BlockItemNode( - item: IntegerLiteralNode(token: tokens[4]), - semicolonToken: tokens[5] + item: IntegerLiteralNode(literal: TokenNode(token: tokens[4])), + semicolon: TokenNode(token: tokens[5]) ) ) ) diff --git a/Tests/ParserTest/FunctionTest.swift b/Tests/ParserTest/FunctionTest.swift index 1541fa7..e021145 100644 --- a/Tests/ParserTest/FunctionTest.swift +++ b/Tests/ParserTest/FunctionTest.swift @@ -29,18 +29,18 @@ final class FunctionTest: XCTestCase { statements: [ BlockItemNode( item: FunctionDeclNode( - returnTypeNode: TypeNode(typeToken: tokens[0]), - functionNameToken: tokens[1], - parenthesisLeftToken: tokens[2], - parameterNodes: [], - parenthesisRightToken: tokens[3], + returnType: TypeNode(type: TokenNode(token: tokens[0])), + functionName: TokenNode(token: tokens[1]), + parenthesisLeft: TokenNode(token: tokens[2]), + parameters: [], + parenthesisRight: TokenNode(token: tokens[3]), block: BlockStatementNode( - braceLeftToken: tokens[4], + braceLeft: TokenNode(token: tokens[4]), items: [ - BlockItemNode(item: IntegerLiteralNode(token: tokens[5]), semicolonToken: tokens[6]), - BlockItemNode(item: IntegerLiteralNode(token: tokens[7]), semicolonToken: tokens[8]), + BlockItemNode(item: IntegerLiteralNode(literal: TokenNode(token: tokens[5])), semicolon: TokenNode(token: tokens[6])), + BlockItemNode(item: IntegerLiteralNode(literal: TokenNode(token: tokens[7])), semicolon: TokenNode(token: tokens[8])), ], - braceRightToken: tokens[9] + braceRight: TokenNode(token: tokens[9]) ) ) ) @@ -75,18 +75,18 @@ final class FunctionTest: XCTestCase { statements: [ BlockItemNode( item: FunctionDeclNode( - returnTypeNode: PointerTypeNode(referenceType: TypeNode(typeToken: tokens[0]), pointerToken: tokens[1]), - functionNameToken: tokens[2], - parenthesisLeftToken: tokens[3], - parameterNodes: [], - parenthesisRightToken: tokens[4], + returnType: PointerTypeNode(referenceType: TypeNode(type: TokenNode(token: tokens[0])), pointer: TokenNode(token: tokens[1])), + functionName: TokenNode(token: tokens[2]), + parenthesisLeft: TokenNode(token: tokens[3]), + parameters: [], + parenthesisRight: TokenNode(token: tokens[4]), block: BlockStatementNode( - braceLeftToken: tokens[5], + braceLeft: TokenNode(token: tokens[5]), items: [ - BlockItemNode(item: IntegerLiteralNode(token: tokens[6]), semicolonToken: tokens[7]), - BlockItemNode(item: IntegerLiteralNode(token: tokens[8]), semicolonToken: tokens[9]), + BlockItemNode(item: IntegerLiteralNode(literal: TokenNode(token: tokens[6])), semicolon: TokenNode(token: tokens[7])), + BlockItemNode(item: IntegerLiteralNode(literal: TokenNode(token: tokens[8])), semicolon: TokenNode(token: tokens[9])), ], - braceRightToken: tokens[10] + braceRight: TokenNode(token: tokens[10]) ) ) ) @@ -112,12 +112,12 @@ final class FunctionTest: XCTestCase { node, BlockItemNode( item: FunctionCallExpressionNode( - identifierToken: tokens[0], - parenthesisLeftToken: tokens[1], + identifier: TokenNode(token: tokens[0]), + parenthesisLeft: TokenNode(token: tokens[1]), arguments: [], - parenthesisRightToken: tokens[2] + parenthesisRight: TokenNode(token: tokens[2]) ), - semicolonToken: tokens[3] + semicolon: TokenNode(token: tokens[3]) ) ) } @@ -150,20 +150,20 @@ final class FunctionTest: XCTestCase { statements: [ BlockItemNode( item: FunctionDeclNode( - returnTypeNode: TypeNode(typeToken: tokens[0]), - functionNameToken: tokens[1], - parenthesisLeftToken: tokens[2], - parameterNodes: [ - FunctionParameterNode(type: TypeNode(typeToken: tokens[3]), identifierToken: tokens[4], commaToken: tokens[5]), - FunctionParameterNode(type: TypeNode(typeToken: tokens[6]), identifierToken: tokens[7]) + returnType: TypeNode(type: TokenNode(token: tokens[0])), + functionName: TokenNode(token: tokens[1]), + parenthesisLeft: TokenNode(token: tokens[2]), + parameters: [ + FunctionParameterNode(type: TypeNode(type: TokenNode(token: tokens[3])), identifier: TokenNode(token: tokens[4]), comma: TokenNode(token: tokens[5])), + FunctionParameterNode(type: TypeNode(type: TokenNode(token: tokens[6])), identifier: TokenNode(token: tokens[7])) ], - parenthesisRightToken: tokens[8], + parenthesisRight: TokenNode(token: tokens[8]), block: BlockStatementNode( - braceLeftToken: tokens[9], + braceLeft: TokenNode(token: tokens[9]), items: [ - BlockItemNode(item: IntegerLiteralNode(token: tokens[10]), semicolonToken: tokens[11]), + BlockItemNode(item: IntegerLiteralNode(literal: TokenNode(token: tokens[10])), semicolon: TokenNode(token: tokens[11])), ], - braceRightToken: tokens[12] + braceRight: TokenNode(token: tokens[12]) ) ) ) @@ -192,18 +192,18 @@ final class FunctionTest: XCTestCase { node, BlockItemNode( item: FunctionCallExpressionNode( - identifierToken: tokens[0], - parenthesisLeftToken: tokens[1], + identifier: TokenNode(token: tokens[0]), + parenthesisLeft: TokenNode(token: tokens[1]), arguments: [ ExpressionListItemNode( - expression: IntegerLiteralNode(token: tokens[2]), - comma: tokens[3] + expression: IntegerLiteralNode(literal: TokenNode(token: tokens[2])), + comma: TokenNode(token: tokens[3]) ), - ExpressionListItemNode(expression: IdentifierNode(token: tokens[4])) + ExpressionListItemNode(expression: IdentifierNode(baseName: TokenNode(token: tokens[4]))) ], - parenthesisRightToken: tokens[5] + parenthesisRight: TokenNode(token: tokens[5]) ), - semicolonToken: tokens[6] + semicolon: TokenNode(token: tokens[6]) ) ) } @@ -239,33 +239,33 @@ final class FunctionTest: XCTestCase { statements: [ BlockItemNode( item: FunctionDeclNode( - returnTypeNode: TypeNode(typeToken: tokens[0]), - functionNameToken: tokens[1], - parenthesisLeftToken: tokens[2], - parameterNodes: [], - parenthesisRightToken: tokens[3], + returnType: TypeNode(type: TokenNode(token: tokens[0])), + functionName: TokenNode(token: tokens[1]), + parenthesisLeft: TokenNode(token: tokens[2]), + parameters: [], + parenthesisRight: TokenNode(token: tokens[3]), block: BlockStatementNode( - braceLeftToken: tokens[4], + braceLeft: TokenNode(token: tokens[4]), items: [ - BlockItemNode(item: IntegerLiteralNode(token: tokens[5]), semicolonToken: tokens[6]) + BlockItemNode(item: IntegerLiteralNode(literal: TokenNode(token: tokens[5])), semicolon: TokenNode(token: tokens[6])) ], - braceRightToken: tokens[7] + braceRight: TokenNode(token: tokens[7]) ) ) ), BlockItemNode( item: FunctionDeclNode( - returnTypeNode: TypeNode(typeToken: tokens[8]), - functionNameToken: tokens[9], - parenthesisLeftToken: tokens[10], - parameterNodes: [], - parenthesisRightToken: tokens[11], + returnType: TypeNode(type: TokenNode(token: tokens[8])), + functionName: TokenNode(token: tokens[9]), + parenthesisLeft: TokenNode(token: tokens[10]), + parameters: [], + parenthesisRight: TokenNode(token: tokens[11]), block: BlockStatementNode( - braceLeftToken: tokens[12], + braceLeft: TokenNode(token: tokens[12]), items: [ - BlockItemNode(item: IntegerLiteralNode(token: tokens[13]), semicolonToken: tokens[14]) + BlockItemNode(item: IntegerLiteralNode(literal: TokenNode(token: tokens[13])), semicolon: TokenNode(token: tokens[14])) ], - braceRightToken: tokens[15] + braceRight: TokenNode(token: tokens[15]) ) ) ) @@ -292,12 +292,12 @@ final class FunctionTest: XCTestCase { node, BlockItemNode( item: SubscriptCallExpressionNode( - identifierNode: IdentifierNode(token: tokens[0]), - squareLeftToken: tokens[1], - argument: IntegerLiteralNode(token: tokens[2]), - squareRightToken: tokens[3] + identifier: IdentifierNode(baseName: TokenNode(token: tokens[0])), + squareLeft: TokenNode(token: tokens[1]), + argument: IntegerLiteralNode(literal: TokenNode(token: tokens[2])), + squareRight: TokenNode(token: tokens[3]) ), - semicolonToken: tokens[4] + semicolon: TokenNode(token: tokens[4]) ) ) } @@ -322,16 +322,16 @@ final class FunctionTest: XCTestCase { node, BlockItemNode( item: SubscriptCallExpressionNode( - identifierNode: IdentifierNode(token: tokens[0]), - squareLeftToken: tokens[1], + identifier: IdentifierNode(baseName: TokenNode(token: tokens[0])), + squareLeft: TokenNode(token: tokens[1]), argument: InfixOperatorExpressionNode( - left: IntegerLiteralNode(token: tokens[2]), - operator: BinaryOperatorNode(token: tokens[3]), - right: IdentifierNode(token: tokens[4]) + left: IntegerLiteralNode(literal: TokenNode(token: tokens[2])), + operator: BinaryOperatorNode(operator: TokenNode(token: tokens[3])), + right: IdentifierNode(baseName: TokenNode(token: tokens[4])) ), - squareRightToken: tokens[5] + squareRight: TokenNode(token: tokens[5]) ), - semicolonToken: tokens[6] + semicolon: TokenNode(token: tokens[6]) ) ) } diff --git a/Tests/ParserTest/OperatorPriorityTest.swift b/Tests/ParserTest/OperatorPriorityTest.swift index 288935e..3637094 100644 --- a/Tests/ParserTest/OperatorPriorityTest.swift +++ b/Tests/ParserTest/OperatorPriorityTest.swift @@ -24,14 +24,14 @@ final class OperatorPriorityTest: XCTestCase { BlockItemNode( item: InfixOperatorExpressionNode( left: InfixOperatorExpressionNode( - left: IntegerLiteralNode(token: tokens[0]), - operator: BinaryOperatorNode(token: tokens[1]), - right: IntegerLiteralNode(token: tokens[2]) + left: IntegerLiteralNode(literal: TokenNode(token: tokens[0])), + operator: BinaryOperatorNode(operator: TokenNode(token: tokens[1])), + right: IntegerLiteralNode(literal: TokenNode(token: tokens[2])) ), - operator: BinaryOperatorNode(token: tokens[3]), - right: IntegerLiteralNode(token: tokens[4]) + operator: BinaryOperatorNode(operator: TokenNode(token: tokens[3])), + right: IntegerLiteralNode(literal: TokenNode(token: tokens[4])) ), - semicolonToken: tokens[5] + semicolon: TokenNode(token: tokens[5]) ) ) } @@ -56,14 +56,14 @@ final class OperatorPriorityTest: XCTestCase { BlockItemNode( item: InfixOperatorExpressionNode( left: InfixOperatorExpressionNode( - left: IntegerLiteralNode(token: tokens[0]), - operator: BinaryOperatorNode(token: tokens[1]), - right: IntegerLiteralNode(token: tokens[2]) + left: IntegerLiteralNode(literal: TokenNode(token: tokens[0])), + operator: BinaryOperatorNode(operator: TokenNode(token: tokens[1])), + right: IntegerLiteralNode(literal: TokenNode(token: tokens[2])) ), - operator: BinaryOperatorNode(token: tokens[3]), - right: IntegerLiteralNode(token: tokens[4]) + operator: BinaryOperatorNode(operator: TokenNode(token: tokens[3])), + right: IntegerLiteralNode(literal: TokenNode(token: tokens[4])) ), - semicolonToken: tokens[5] + semicolon: TokenNode(token: tokens[5]) ) ) } @@ -87,15 +87,15 @@ final class OperatorPriorityTest: XCTestCase { node, BlockItemNode( item: InfixOperatorExpressionNode( - left: IntegerLiteralNode(token: tokens[0]), - operator: BinaryOperatorNode(token: tokens[1]), + left: IntegerLiteralNode(literal: TokenNode(token: tokens[0])), + operator: BinaryOperatorNode(operator: TokenNode(token: tokens[1])), right: InfixOperatorExpressionNode( - left: IntegerLiteralNode(token: tokens[2]), - operator: BinaryOperatorNode(token: tokens[3]), - right: IntegerLiteralNode(token: tokens[4]) + left: IntegerLiteralNode(literal: TokenNode(token: tokens[2])), + operator: BinaryOperatorNode(operator: TokenNode(token: tokens[3])), + right: IntegerLiteralNode(literal: TokenNode(token: tokens[4])) ) ), - semicolonToken: tokens[5] + semicolon: TokenNode(token: tokens[5]) ) ) } @@ -122,18 +122,18 @@ final class OperatorPriorityTest: XCTestCase { BlockItemNode( item: InfixOperatorExpressionNode( left: TupleExpressionNode( - parenthesisLeftToken: tokens[0], + parenthesisLeft: TokenNode(token: tokens[0]), expression: InfixOperatorExpressionNode( - left: IntegerLiteralNode(token: tokens[1]), - operator: BinaryOperatorNode(token: tokens[2]), - right: IntegerLiteralNode(token: tokens[3]) + left: IntegerLiteralNode(literal: TokenNode(token: tokens[1])), + operator: BinaryOperatorNode(operator: TokenNode(token: tokens[2])), + right: IntegerLiteralNode(literal: TokenNode(token: tokens[3])) ), - parenthesisRightToken: tokens[4] + parenthesisRight: TokenNode(token: tokens[4]) ), - operator: BinaryOperatorNode(token: tokens[5]), - right: IntegerLiteralNode(token: tokens[6]) + operator: BinaryOperatorNode(operator: TokenNode(token: tokens[5])), + right: IntegerLiteralNode(literal: TokenNode(token: tokens[6])) ), - semicolonToken: tokens[7] + semicolon: TokenNode(token: tokens[7]) ) ) } diff --git a/Tests/ParserTest/OperatorsTest.swift b/Tests/ParserTest/OperatorsTest.swift index 5e8c6f5..4b239ac 100644 --- a/Tests/ParserTest/OperatorsTest.swift +++ b/Tests/ParserTest/OperatorsTest.swift @@ -21,11 +21,11 @@ final class OperatorsTest: XCTestCase { node, BlockItemNode( item: InfixOperatorExpressionNode( - left: IntegerLiteralNode(token: tokens[0]), - operator: BinaryOperatorNode(token: tokens[1]), - right: IntegerLiteralNode(token: tokens[2]) + left: IntegerLiteralNode(literal: TokenNode(token: tokens[0])), + operator: BinaryOperatorNode(operator: TokenNode(token: tokens[1])), + right: IntegerLiteralNode(literal: TokenNode(token: tokens[2])) ), - semicolonToken: tokens[3] + semicolon: TokenNode(token: tokens[3]) ) ) } @@ -47,11 +47,11 @@ final class OperatorsTest: XCTestCase { node, BlockItemNode( item: InfixOperatorExpressionNode( - left: IntegerLiteralNode(token: tokens[0]), - operator: BinaryOperatorNode(token: tokens[1]), - right: IntegerLiteralNode(token: tokens[2]) + left: IntegerLiteralNode(literal: TokenNode(token: tokens[0])), + operator: BinaryOperatorNode(operator: TokenNode(token: tokens[1])), + right: IntegerLiteralNode(literal: TokenNode(token: tokens[2])) ), - semicolonToken: tokens[3] + semicolon: TokenNode(token: tokens[3]) ) ) } @@ -73,11 +73,11 @@ final class OperatorsTest: XCTestCase { node, BlockItemNode( item: InfixOperatorExpressionNode( - left: IntegerLiteralNode(token: tokens[0]), - operator: BinaryOperatorNode(token: tokens[1]), - right: IntegerLiteralNode(token: tokens[2]) + left: IntegerLiteralNode(literal: TokenNode(token: tokens[0])), + operator: BinaryOperatorNode(operator: TokenNode(token: tokens[1])), + right: IntegerLiteralNode(literal: TokenNode(token: tokens[2])) ), - semicolonToken: tokens[3] + semicolon: TokenNode(token: tokens[3]) ) ) } @@ -99,11 +99,11 @@ final class OperatorsTest: XCTestCase { node, BlockItemNode( item: InfixOperatorExpressionNode( - left: IntegerLiteralNode(token: tokens[0]), - operator: BinaryOperatorNode(token: tokens[1]), - right: IntegerLiteralNode(token: tokens[2]) + left: IntegerLiteralNode(literal: TokenNode(token: tokens[0])), + operator: BinaryOperatorNode(operator: TokenNode(token: tokens[1])), + right: IntegerLiteralNode(literal: TokenNode(token: tokens[2])) ), - semicolonToken: tokens[3] + semicolon: TokenNode(token: tokens[3]) ) ) } @@ -124,10 +124,10 @@ final class OperatorsTest: XCTestCase { node, BlockItemNode( item: PrefixOperatorExpressionNode( - operator: tokens[0], - expression: IntegerLiteralNode(token: tokens[1]) + operator: TokenNode(token: tokens[0]), + expression: IntegerLiteralNode(literal: TokenNode(token: tokens[1])) ), - semicolonToken: tokens[2] + semicolon: TokenNode(token: tokens[2]) ) ) } @@ -148,10 +148,10 @@ final class OperatorsTest: XCTestCase { node, BlockItemNode( item: PrefixOperatorExpressionNode( - operator: tokens[0], - expression: IntegerLiteralNode(token: tokens[1]) + operator: TokenNode(token: tokens[0]), + expression: IntegerLiteralNode(literal: TokenNode(token: tokens[1])) ), - semicolonToken: tokens[2] + semicolon: TokenNode(token: tokens[2]) ) ) } @@ -172,10 +172,10 @@ final class OperatorsTest: XCTestCase { node, BlockItemNode( item: PrefixOperatorExpressionNode( - operator: tokens[0], - expression: IdentifierNode(token: tokens[1]) + operator: TokenNode(token: tokens[0]), + expression: IdentifierNode(baseName: TokenNode(token: tokens[1])) ), - semicolonToken: tokens[2] + semicolon: TokenNode(token: tokens[2]) ) ) } @@ -196,10 +196,10 @@ final class OperatorsTest: XCTestCase { node, BlockItemNode( item: PrefixOperatorExpressionNode( - operator: tokens[0], - expression: IdentifierNode(token: tokens[1]) + operator: TokenNode(token: tokens[0]), + expression: IdentifierNode(baseName: TokenNode(token: tokens[1])) ), - semicolonToken: tokens[2] + semicolon: TokenNode(token: tokens[2]) ) ) } @@ -221,11 +221,11 @@ final class OperatorsTest: XCTestCase { node, BlockItemNode( item: InfixOperatorExpressionNode( - left: IntegerLiteralNode(token: tokens[0]), - operator: BinaryOperatorNode(token: tokens[1]), - right: IntegerLiteralNode(token: tokens[2]) + left: IntegerLiteralNode(literal: TokenNode(token: tokens[0])), + operator: BinaryOperatorNode(operator: TokenNode(token: tokens[1])), + right: IntegerLiteralNode(literal: TokenNode(token: tokens[2])) ), - semicolonToken: tokens[3] + semicolon: TokenNode(token: tokens[3]) ) ) } @@ -247,11 +247,11 @@ final class OperatorsTest: XCTestCase { node, BlockItemNode( item: InfixOperatorExpressionNode( - left: IntegerLiteralNode(token: tokens[0]), - operator: BinaryOperatorNode(token: tokens[1]), - right: IntegerLiteralNode(token: tokens[2]) + left: IntegerLiteralNode(literal: TokenNode(token: tokens[0])), + operator: BinaryOperatorNode(operator: TokenNode(token: tokens[1])), + right: IntegerLiteralNode(literal: TokenNode(token: tokens[2])) ), - semicolonToken: tokens[3] + semicolon: TokenNode(token: tokens[3]) ) ) } @@ -273,11 +273,11 @@ final class OperatorsTest: XCTestCase { node, BlockItemNode( item: InfixOperatorExpressionNode( - left: IntegerLiteralNode(token: tokens[0]), - operator: BinaryOperatorNode(token: tokens[1]), - right: IntegerLiteralNode(token: tokens[2]) + left: IntegerLiteralNode(literal: TokenNode(token: tokens[0])), + operator: BinaryOperatorNode(operator: TokenNode(token: tokens[1])), + right: IntegerLiteralNode(literal: TokenNode(token: tokens[2])) ), - semicolonToken: tokens[3] + semicolon: TokenNode(token: tokens[3]) ) ) } @@ -299,11 +299,11 @@ final class OperatorsTest: XCTestCase { node, BlockItemNode( item: InfixOperatorExpressionNode( - left: IntegerLiteralNode(token: tokens[0]), - operator: BinaryOperatorNode(token: tokens[1]), - right: IntegerLiteralNode(token: tokens[2]) + left: IntegerLiteralNode(literal: TokenNode(token: tokens[0])), + operator: BinaryOperatorNode(operator: TokenNode(token: tokens[1])), + right: IntegerLiteralNode(literal: TokenNode(token: tokens[2])) ), - semicolonToken: tokens[3] + semicolon: TokenNode(token: tokens[3]) ) ) } @@ -325,11 +325,11 @@ final class OperatorsTest: XCTestCase { node, BlockItemNode( item: InfixOperatorExpressionNode( - left: IntegerLiteralNode(token: tokens[0]), - operator: BinaryOperatorNode(token: tokens[1]), - right: IntegerLiteralNode(token: tokens[2]) + left: IntegerLiteralNode(literal: TokenNode(token: tokens[0])), + operator: BinaryOperatorNode(operator: TokenNode(token: tokens[1])), + right: IntegerLiteralNode(literal: TokenNode(token: tokens[2])) ), - semicolonToken: tokens[3] + semicolon: TokenNode(token: tokens[3]) ) ) } @@ -351,11 +351,11 @@ final class OperatorsTest: XCTestCase { node, BlockItemNode( item: InfixOperatorExpressionNode( - left: IntegerLiteralNode(token: tokens[0]), - operator: BinaryOperatorNode(token: tokens[1]), - right: IntegerLiteralNode(token: tokens[2]) + left: IntegerLiteralNode(literal: TokenNode(token: tokens[0])), + operator: BinaryOperatorNode(operator: TokenNode(token: tokens[1])), + right: IntegerLiteralNode(literal: TokenNode(token: tokens[2])) ), - semicolonToken: tokens[3] + semicolon: TokenNode(token: tokens[3]) ) ) } @@ -376,8 +376,16 @@ final class OperatorsTest: XCTestCase { XCTAssertEqual( node, BlockItemNode( - item: IntegerLiteralNode(token: Token(kind: .number("8"), sourceRange: SourceRange(start: SourceLocation(line: 1, column: 1), end: SourceLocation(line: 1, column: 1)))), - semicolonToken: tokens[4] + item: IntegerLiteralNode( + literal: TokenNode(token: Token( + kind: .number("8"), + sourceRange: SourceRange( + start: SourceLocation(line: 1, column: 1), + end: SourceLocation(line: 1, column: 1) + ) + )) + ), + semicolon: TokenNode(token: tokens[4]) ) ) } diff --git a/Tests/ParserTest/ReturnTest.swift b/Tests/ParserTest/ReturnTest.swift index ca20ec1..b79f4b2 100644 --- a/Tests/ParserTest/ReturnTest.swift +++ b/Tests/ParserTest/ReturnTest.swift @@ -20,10 +20,10 @@ final class ReturnTest: XCTestCase { node, BlockItemNode( item: ReturnStatementNode( - returnToken: tokens[0], - expression: IntegerLiteralNode(token: tokens[1]) + return: TokenNode(token: tokens[0]), + expression: IntegerLiteralNode(literal: TokenNode(token: tokens[1])) ), - semicolonToken: tokens[2] + semicolon: TokenNode(token: tokens[2]) ) ) } diff --git a/Tests/ParserTest/StringLiteralTest.swift b/Tests/ParserTest/StringLiteralTest.swift index 5174d1c..b38bbd6 100644 --- a/Tests/ParserTest/StringLiteralTest.swift +++ b/Tests/ParserTest/StringLiteralTest.swift @@ -18,8 +18,8 @@ final class StringLiteralTest: XCTestCase { XCTAssertEqual( node, BlockItemNode( - item: StringLiteralNode(token: tokens[0]), - semicolonToken: tokens[1] + item: StringLiteralNode(literal: TokenNode(token: tokens[0])), + semicolon: TokenNode(token: tokens[1]) ) ) } @@ -41,11 +41,11 @@ final class StringLiteralTest: XCTestCase { node, BlockItemNode( item: InfixOperatorExpressionNode( - left: IdentifierNode(token: tokens[0]), - operator: AssignNode(token: tokens[1]), - right: StringLiteralNode(token: tokens[2]) + left: IdentifierNode(baseName: TokenNode(token: tokens[0])), + operator: AssignNode(equal: TokenNode(token: tokens[1])), + right: StringLiteralNode(literal: TokenNode(token: tokens[2])) ), - semicolonToken: tokens[3] + semicolon: TokenNode(token: tokens[3]) ) ) } diff --git a/Tests/ParserTest/VariableTest.swift b/Tests/ParserTest/VariableTest.swift index 63b58bf..de21949 100644 --- a/Tests/ParserTest/VariableTest.swift +++ b/Tests/ParserTest/VariableTest.swift @@ -20,10 +20,10 @@ final class VariableTest: XCTestCase { node, BlockItemNode( item: VariableDeclNode( - type: TypeNode(typeToken: tokens[0]), - identifierToken: tokens[1] + type: TypeNode(type: TokenNode(token: tokens[0])), + identifier: TokenNode(token: tokens[1]) ), - semicolonToken: tokens[2] + semicolon: TokenNode(token: tokens[2]) ) ) } @@ -46,12 +46,12 @@ final class VariableTest: XCTestCase { node, BlockItemNode( item: VariableDeclNode( - type: TypeNode(typeToken: tokens[0]), - identifierToken: tokens[1], - initializerToken: tokens[2], - initializerExpr: IntegerLiteralNode(token: tokens[3]) + type: TypeNode(type: TokenNode(token: tokens[0])), + identifier: TokenNode(token: tokens[1]), + equal: TokenNode(token: tokens[2]), + initializerExpr: IntegerLiteralNode(literal: TokenNode(token: tokens[3])) ), - semicolonToken: tokens[4] + semicolon: TokenNode(token: tokens[4]) ) ) } @@ -74,12 +74,12 @@ final class VariableTest: XCTestCase { BlockItemNode( item: VariableDeclNode( type: PointerTypeNode( - referenceType: TypeNode(typeToken: tokens[0]), - pointerToken: tokens[1] + referenceType: TypeNode(type: TokenNode(token: tokens[0])), + pointer: TokenNode(token: tokens[1]) ), - identifierToken: tokens[2] + identifier: TokenNode(token: tokens[2]) ), - semicolonToken: tokens[3] + semicolon: TokenNode(token: tokens[3]) ) ) } @@ -104,14 +104,14 @@ final class VariableTest: XCTestCase { item: VariableDeclNode( type: PointerTypeNode( referenceType: PointerTypeNode( - referenceType: TypeNode(typeToken: tokens[0]), - pointerToken: tokens[1] + referenceType: TypeNode(type: TokenNode(token: tokens[0])), + pointer: TokenNode(token: tokens[1]) ), - pointerToken: tokens[2] + pointer: TokenNode(token: tokens[2]) ), - identifierToken: tokens[3] + identifier: TokenNode(token: tokens[3]) ), - semicolonToken: tokens[4] + semicolon: TokenNode(token: tokens[4]) ) ) } @@ -135,14 +135,14 @@ final class VariableTest: XCTestCase { BlockItemNode( item: VariableDeclNode( type: ArrayTypeNode( - elementType: TypeNode(typeToken: tokens[0]), - squareLeftToken: tokens[2], - arraySizeToken: tokens[3], - squareRightToken: tokens[4] + elementType: TypeNode(type: TokenNode(token: tokens[0])), + squareLeft: TokenNode(token: tokens[2]), + arraySize: TokenNode(token: tokens[3]), + squareRight: TokenNode(token: tokens[4]) ), - identifierToken: tokens[1] + identifier: TokenNode(token: tokens[1]) ), - semicolonToken: tokens[5] + semicolon: TokenNode(token: tokens[5]) ) ) } @@ -172,23 +172,23 @@ final class VariableTest: XCTestCase { BlockItemNode( item: VariableDeclNode( type: ArrayTypeNode( - elementType: TypeNode(typeToken: tokens[0]), - squareLeftToken: tokens[2], - arraySizeToken: tokens[3], - squareRightToken: tokens[4] + elementType: TypeNode(type: TokenNode(token: tokens[0])), + squareLeft: TokenNode(token: tokens[2]), + arraySize: TokenNode(token: tokens[3]), + squareRight: TokenNode(token: tokens[4]) ), - identifierToken: tokens[1], - initializerToken: tokens[5], + identifier: TokenNode(token: tokens[1]), + equal: TokenNode(token: tokens[5]), initializerExpr: ArrayExpressionNode( - braceLeft: tokens[6], + braceLeft: TokenNode(token: tokens[6]), exprListNodes: [ - ExpressionListItemNode(expression: IntegerLiteralNode(token: tokens[7]), comma: tokens[8]), - ExpressionListItemNode(expression: IntegerLiteralNode(token: tokens[9])) + ExpressionListItemNode(expression: IntegerLiteralNode(literal: TokenNode(token: tokens[7])), comma: TokenNode(token: tokens[8])), + ExpressionListItemNode(expression: IntegerLiteralNode(literal: TokenNode(token: tokens[9]))) ], - braceRight: tokens[10] + braceRight: TokenNode(token: tokens[10]) ) ), - semicolonToken: tokens[11] + semicolon: TokenNode(token: tokens[11]) ) ) } @@ -214,16 +214,16 @@ final class VariableTest: XCTestCase { BlockItemNode( item: VariableDeclNode( type: ArrayTypeNode( - elementType: TypeNode(typeToken: tokens[0]), - squareLeftToken: tokens[2], - arraySizeToken: tokens[3], - squareRightToken: tokens[4] + elementType: TypeNode(type: TokenNode(token: tokens[0])), + squareLeft: TokenNode(token: tokens[2]), + arraySize: TokenNode(token: tokens[3]), + squareRight: TokenNode(token: tokens[4]) ), - identifierToken: tokens[1], - initializerToken: tokens[5], - initializerExpr: StringLiteralNode(token: tokens[6]) + identifier: TokenNode(token: tokens[1]), + equal: TokenNode(token: tokens[5]), + initializerExpr: StringLiteralNode(literal: TokenNode(token: tokens[6])) ), - semicolonToken: tokens[7] + semicolon: TokenNode(token: tokens[7]) ) ) } @@ -248,14 +248,14 @@ final class VariableTest: XCTestCase { BlockItemNode( item: VariableDeclNode( type: ArrayTypeNode( - elementType: PointerTypeNode(referenceType: TypeNode(typeToken: tokens[0]), pointerToken: tokens[1]), - squareLeftToken: tokens[3], - arraySizeToken: tokens[4], - squareRightToken: tokens[5] + elementType: PointerTypeNode(referenceType: TypeNode(type: TokenNode(token: tokens[0])), pointer: TokenNode(token: tokens[1])), + squareLeft: TokenNode(token: tokens[3]), + arraySize: TokenNode(token: tokens[4]), + squareRight: TokenNode(token: tokens[5]) ), - identifierToken: tokens[2] + identifier: TokenNode(token: tokens[2]) ), - semicolonToken: tokens[6] + semicolon: TokenNode(token: tokens[6]) ) ) } @@ -278,10 +278,10 @@ final class VariableTest: XCTestCase { statements: [ BlockItemNode( item: VariableDeclNode( - type: TypeNode(typeToken: tokens[0]), - identifierToken: tokens[1] + type: TypeNode(type: TokenNode(token: tokens[0])), + identifier: TokenNode(token: tokens[1]) ), - semicolonToken: tokens[2] + semicolon: TokenNode(token: tokens[2]) ) ] ) @@ -308,12 +308,12 @@ final class VariableTest: XCTestCase { BlockItemNode( item: VariableDeclNode( type: PointerTypeNode( - referenceType: TypeNode(typeToken: tokens[0]), - pointerToken: tokens[1] + referenceType: TypeNode(type: TokenNode(token: tokens[0])), + pointer: TokenNode(token: tokens[1]) ), - identifierToken: tokens[2] + identifier: TokenNode(token: tokens[2]) ), - semicolonToken: tokens[3] + semicolon: TokenNode(token: tokens[3]) ) ] ) diff --git a/Tests/TokenizerTest/StringLiteralTest.swift b/Tests/TokenizerTest/StringLiteralTest.swift index e4ac34e..6ff80f5 100644 --- a/Tests/TokenizerTest/StringLiteralTest.swift +++ b/Tests/TokenizerTest/StringLiteralTest.swift @@ -7,8 +7,7 @@ final class StringLiteralTest: XCTestCase { let source = "\"aaaa\"" let tokens = try Tokenizer(source: source).tokenize() - // FIXME: StringLiteralの両端のクオーテーションをどうするか - XCTAssertEqual(tokens.reduce("") { $0 + $1.description }, "aaaa") + XCTAssertEqual(tokens.reduce("") { $0 + $1.description }, source) XCTAssertEqual( tokens, [