Skip to content

Commit

Permalink
Fix all nodes have TokenNode instead of Token
Browse files Browse the repository at this point in the history
  • Loading branch information
kntkymt committed Jan 8, 2024
1 parent 036a0fe commit 7b0e85a
Show file tree
Hide file tree
Showing 21 changed files with 644 additions and 707 deletions.
99 changes: 55 additions & 44 deletions Sources/Generator/Generator.swift

Large diffs are not rendered by default.

97 changes: 39 additions & 58 deletions Sources/Parser/Node/ExpressionNode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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]
}
Expand Down Expand Up @@ -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

Expand All @@ -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
}
Expand All @@ -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
}
}

Expand All @@ -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
}
}

Expand All @@ -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
Expand All @@ -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
}
Expand All @@ -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
}
}
8 changes: 8 additions & 0 deletions Sources/Parser/Node/Node.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import Tokenizer

public enum NodeKind {
case token

case integerLiteral
case identifier
case stringLiteral
Expand Down Expand Up @@ -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)
Expand Down
91 changes: 52 additions & 39 deletions Sources/Parser/Node/PrimaryNode.swift
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
}
}

Expand All @@ -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
}
}

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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`
}
}

Expand All @@ -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
}
}
Loading

0 comments on commit 7b0e85a

Please sign in to comment.