Commit 3b08b1a8 authored by David Owens II's avatar David Owens II

Fixed an issue with comments that ended at the EOF marker.

parent 3139af76
Pipeline #671 passed with stage
......@@ -25,7 +25,6 @@ public enum Token {
case StringLiteral(String, line: Int, column: Int)
case Terminal(line: Int, column: Int)
case Colon(line: Int, column: Int)
case SemiColon(line: Int, column: Int)
case Comment(String, line: Int, column: Int)
case Unhandled(String, line: Int, column: Int)
......@@ -59,7 +58,6 @@ public enum Token {
case let (.CloseBrace(l0, l1), .CloseBrace(r0, r1)): return l0 == r0 && l1 == r1
case let (.Terminal(l0, l1), .Terminal(r0, r1)): return l0 == r0 && l1 == r1
case let (.Colon(l0, l1), .Colon(r0, r1)): return l0 == r0 && l1 == r1
case let (.SemiColon(l0, l1), .SemiColon(r0, r1)): return l0 == r0 && l1 == r1
case (.EOF, .EOF): return true
......@@ -145,19 +143,18 @@ public class Lexer {
return .Colon(line: next.line, column: next.column)
}
else if next.character == ";" {
if scanner.next()?.character == ";" {
let column = scanner.peek()!.column - 1
var comment = ""
while let info = scanner.next() where info.character != "\n" {
comment.append(info.character!)
}
return .Comment(comment, line: scanner.peek()!.line, column: column)
}
else {
scanner.stall()
return .SemiColon(line: next.line, column: next.column)
let column = scanner.peek()!.column
let line = scanner.peek()!.line
var comment = ""
while let info = scanner.next() where info.character == ";" {}
scanner.stall()
while let info = scanner.next() where info.character != "\n" {
comment.append(info.character!)
}
return .Comment(comment, line: line, column: column)
}
else if next.character == "\"" {
var content = String(next.character!)
......@@ -194,15 +191,4 @@ public class Lexer {
public func peek() -> Token? {
return current
}
public func debugPrint() {
print("--- TOKENS ---")
while let token = self.next() {
print("\(token)")
}
print("")
self.scanner._defaults()
self.current = nil
}
}
\ No newline at end of file
......@@ -16,6 +16,36 @@
import Foundation
import atpkgparser
func outputBaseline(lexer: Lexer) {
print("--- baseline ---")
while let token = lexer.next() {
var output = ""
switch token {
case let .OpenParen(line, column): output = "try test.assert(Token.isEqual(lexer.next(), to: Token.OpenParen(line: \(line), column: \(column))))"
case let .CloseParen(line, column): output = "try test.assert(Token.isEqual(lexer.next(), to: Token.CloseParen(line: \(line), column: \(column))))"
case let .OpenBracket(line, column): output = "try test.assert(Token.isEqual(lexer.next(), to: Token.OpenBracket(line: \(line), column: \(column))))"
case let .CloseBracket(line, column): output = "try test.assert(Token.isEqual(lexer.next(), to: Token.CloseBracket(line: \(line), column: \(column))))"
case let .OpenBrace(line, column): output = "try test.assert(Token.isEqual(lexer.next(), to: Token.OpenBrace(line: \(line), column: \(column))))"
case let .CloseBrace(line, column): output = "try test.assert(Token.isEqual(lexer.next(), to: Token.CloseBrace(line: \(line), column: \(column))))"
case let .Terminal(line, column): output = "try test.assert(Token.isEqual(lexer.next(), to: Token.Terminal(line: \(line), column: \(column))))"
case let .Colon(line, column): output = "try test.assert(Token.isEqual(lexer.next(), to: Token.Colon(line: \(line), column: \(column))))"
case let .Identifier(str, line, column): output = "try test.assert(Token.isEqual(lexer.next(), to: Token.Identifier(\"\(str)\", line: \(line), column: \(column))))"
case let .StringLiteral(str, line, column):
let escapedStr = str.stringByReplacingOccurrencesOfString("\"", withString: "\\\"")
output = "try test.assert(Token.isEqual(lexer.next(), to: Token.StringLiteral(\"\(escapedStr)\", line: \(line), column: \(column))))"
case let .Comment(str, line, column): output = "try test.assert(Token.isEqual(lexer.next(), to: Token.Comment(\"\(str)\", line: \(line), column: \(column))))"
case let .Unhandled(str, line, column): output = "try test.assert(Token.isEqual(lexer.next(), to: Token.Unhandled(\"\(str)\", line: \(line), column: \(column))))"
case .EOF: output = "try test.assert(Token.isEqual(lexer.next(), to: Token.EOF))"
}
print(output)
}
print("--- end baseline ---")
}
class LexerTests: Test {
required init() {}
let tests = [
......@@ -73,6 +103,8 @@ class LexerTests: Test {
try test.assert(Token.isEqual(lexer.next(), to: Token.Terminal(line: 9, column: 46)))
try test.assert(Token.isEqual(lexer.next(), to: Token.CloseParen(line: 10, column: 0)))
try test.assert(Token.isEqual(lexer.next(), to: Token.Terminal(line: 10, column: 1)))
try test.assert(Token.isEqual(lexer.next(), to: Token.Terminal(line: 11, column: 0)))
try test.assert(Token.isEqual(lexer.next(), to: Token.Comment(" End of the sample.", line: 12, column: 0)))
try test.assert(Token.isEqual(lexer.next(), to: Token.EOF))
}
}
\ No newline at end of file
......@@ -305,5 +305,26 @@ class ScannerTests: Test {
try test.assert(scanner.next()?.character == "\n")
try test.assert(scanner.next()?.character == ")")
try test.assert(scanner.next()?.character == "\n")
try test.assert(scanner.next()?.character == "\n")
try test.assert(scanner.next()?.character == ";")
try test.assert(scanner.next()?.character == " ")
try test.assert(scanner.next()?.character == "E")
try test.assert(scanner.next()?.character == "n")
try test.assert(scanner.next()?.character == "d")
try test.assert(scanner.next()?.character == " ")
try test.assert(scanner.next()?.character == "o")
try test.assert(scanner.next()?.character == "f")
try test.assert(scanner.next()?.character == " ")
try test.assert(scanner.next()?.character == "t")
try test.assert(scanner.next()?.character == "h")
try test.assert(scanner.next()?.character == "e")
try test.assert(scanner.next()?.character == " ")
try test.assert(scanner.next()?.character == "s")
try test.assert(scanner.next()?.character == "a")
try test.assert(scanner.next()?.character == "m")
try test.assert(scanner.next()?.character == "p")
try test.assert(scanner.next()?.character == "l")
try test.assert(scanner.next()?.character == "e")
try test.assert(scanner.next()?.character == ".")
}
}
\ No newline at end of file
......@@ -9,3 +9,5 @@
:output-type "lib"
:source [ "src/**.swift" ]}}
)
; End of the sample.
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment