Skip to content

Commit

Permalink
Grouping, assignment tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mattmassicotte committed May 11, 2023
1 parent 5772d3a commit e1a8f72
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ concatenation = a, b, c;
alternation = a | b | c;
optional = [a, b];
repetition = {a};
grouping = (a, b, c);
```

## Usage
Expand Down
16 changes: 16 additions & 0 deletions Sources/Gramophone/BNFParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,20 @@ public class BNFParser {
return .repetition(rule)
}

private func parseGrouping(_ lexer: BNFLexerReference) throws -> Rule.Kind {
guard let _ = lexer.nextIf({ $0.kind == .openParen }) else {
throw BNFParserError.unexpectedToken
}

let rule = try parsePrimaryExpression(lexer)

guard let _ = lexer.nextIf({ $0.kind == .closeParen }) else {
throw BNFParserError.unexpectedToken
}

return .grouping(rule)
}

private func parsePrimaryExpression(_ lexer: BNFLexerReference) throws -> Rule.Kind {
switch lexer.peek()?.kind {
case .quote, .doubleQuote:
Expand All @@ -133,6 +147,8 @@ public class BNFParser {
return try parseReference(lexer)
case .openBrace:
return try parseRepetition(lexer)
case .openParen:
return try parseGrouping(lexer)
default:
throw BNFParserError.unexpectedToken
}
Expand Down
3 changes: 2 additions & 1 deletion Sources/Gramophone/Rule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ public struct Rule {
case alternation(Kind, Kind)
case optional(Kind)
case repetition(Kind)
case grouping
case grouping(Kind)
case terminalString(String)
case oneOrMore
case comment
case specialSequence
case exception
Expand Down
39 changes: 39 additions & 0 deletions Tests/GramophoneTests/ParserTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,32 @@ import XCTest
@testable import Gramophone

final class ParserTests: XCTestCase {
func testAssignment() throws {
let string = "test = 'a';"
let parser = BNFParser()

let rules = try parser.parse(string).get()

XCTAssertEqual(rules.count, 1)

let expectedRule = Rule(name: "test",
kind: .terminalString("a"))
XCTAssertEqual(rules[0], expectedRule)
}

func testAssignmentWithArrow() throws {
let string = "test → 'a';"
let parser = BNFParser()

let rules = try parser.parse(string).get()

XCTAssertEqual(rules.count, 1)

let expectedRule = Rule(name: "test",
kind: .terminalString("a"))
XCTAssertEqual(rules[0], expectedRule)
}

func testAlternation() throws {
let string = "test = 'a' | 'b';"
let parser = BNFParser()
Expand Down Expand Up @@ -53,4 +79,17 @@ final class ParserTests: XCTestCase {
kind: .repetition(.terminalString("a")))
XCTAssertEqual(rules[0], expectedRule)
}

func testGrouping() throws {
let string = "test = ('a');"
let parser = BNFParser()

let rules = try parser.parse(string).get()

XCTAssertEqual(rules.count, 1)

let expectedRule = Rule(name: "test",
kind: .grouping(.terminalString("a")))
XCTAssertEqual(rules[0], expectedRule)
}
}

0 comments on commit e1a8f72

Please sign in to comment.