Skip to content

Commit

Permalink
Add ignoring white space, tab(\t) carriage return (\r) and new line (…
Browse files Browse the repository at this point in the history
…\n) characters
  • Loading branch information
AnasMostefaoui committed Mar 7, 2018
1 parent cfe4c7a commit 23d0979
Show file tree
Hide file tree
Showing 2 changed files with 150 additions and 3 deletions.
18 changes: 16 additions & 2 deletions LoxInterpreter/LoxScanner.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ public class LoxScanner : ScannerInterface {
}

let lookAheadOffset = 1
let aheadCharacters = self.cursor.lookAhead(by: lookAheadOffset) ?? ""
_ = self.cursor.lookAhead(by: lookAheadOffset) ?? ""
let currentCharacter = String(character)

switch String(currentCharacter) {

// comments
case TokenType.slash.rawValue where match(character: "/"):
ignoreComment()
Expand All @@ -71,6 +71,11 @@ public class LoxScanner : ScannerInterface {
self.addToken(tokenType: TokenType.notEqual)
// single characters
case _ where currentCharacter.count == 1 :
// ignore new line, space, tab characters
guard shouldIgnore(character: currentCharacter) == false else {
return
}

guard let tokenType = TokenType(rawValue: currentCharacter) else {
fallthrough
}
Expand Down Expand Up @@ -108,6 +113,15 @@ public class LoxScanner : ScannerInterface {
}
}

func shouldIgnore(character:String) -> Bool {
switch character {
case " ", "\r", "\t", "\n":
return true
default:
return false
}
}

func addToken(tokenType:TokenType) {
let lexem = self.cursor.getCurrentLexem()
let token = Token(type: tokenType, lexem: "\(lexem)", literal: nil, line: self.cursor.line)
Expand Down
135 changes: 134 additions & 1 deletion LoxInterpreterTests/UnitTests/ScannerTests/ScannerUnitTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ class ScannerUnitTests: XCTestCase {
XCTAssertTrue(itHasError)
}

// comments and slash tests
func test_if_it_can_detect_comments() {
var itHasNotEqual = false
var itHasGreaterThan = false
Expand All @@ -161,7 +162,7 @@ class ScannerUnitTests: XCTestCase {
XCTAssertTrue(errors.isEmpty)
}

func test_if_it_can_detect_nexted_inline_comments() {
func test_if_it_can_detect_nested_inline_comments() {
var itHasNotEqual = false
var itHasGreaterThan = false

Expand All @@ -183,6 +184,138 @@ class ScannerUnitTests: XCTestCase {
XCTAssertTrue(errors.isEmpty)
}

func test_if_it_can_detect_slash_and_comments() {

let source =
"""
!=//yo whats app ? //first line
/
//third line
//forthline
"""
var itHasNotEqual = false
var itHasSlash = false

scanner.source = source
let tokens = scanner.scan()
let errors = scanner.errors

tokens.forEach {
if $0.type == TokenType.notEqual {
itHasNotEqual = true
} else if $0.type == TokenType.slash {
itHasSlash = true
}
}

XCTAssertTrue(itHasSlash)
XCTAssertTrue(itHasNotEqual)
XCTAssertEqual(tokens.count, 3)
XCTAssertTrue(errors.isEmpty)
}

// white space, \t \r \n
func test_if_it_can_ignore_white_space() {
let source =
"""
!= >=
//
"""
let expectedLines = 2
var itHasNotEqual = false
var itHasGreaterThan = false


scanner.source = source
let tokens = scanner.scan()
let errors = scanner.errors

tokens.forEach {
if $0.type == TokenType.notEqual {
itHasNotEqual = true
} else if $0.type == TokenType.greaterOrEqual {
itHasGreaterThan = true
}
}

let lastToken = tokens[tokens.index(before: tokens.endIndex)]

XCTAssertEqual(lastToken.line, expectedLines)

XCTAssertTrue(itHasGreaterThan)
XCTAssertTrue(itHasNotEqual)

XCTAssertEqual(tokens.count, 3)
XCTAssertTrue(errors.isEmpty)
}

func test_if_it_can_ignore_carriage_return() {
let source =
"""
!= \r \r \t \t >=
"""
let expectedLines = 1
var itHasNotEqual = false
var itHasGreaterThan = false


scanner.source = source
let tokens = scanner.scan()
let errors = scanner.errors

tokens.forEach {
if $0.type == TokenType.notEqual {
itHasNotEqual = true
} else if $0.type == TokenType.greaterOrEqual {
itHasGreaterThan = true
}
}

let lastToken = tokens[tokens.index(before: tokens.endIndex)]

XCTAssertEqual(lastToken.line, expectedLines)

XCTAssertTrue(itHasGreaterThan)
XCTAssertTrue(itHasNotEqual)

XCTAssertEqual(tokens.count, 3)
XCTAssertTrue(errors.isEmpty)
}

func test_if_it_can_ignore_tab_space() {
let source =
"""
!= \t >= // first line
"""
let expectedLines = 1
var itHasNotEqual = false
var itHasGreaterThan = false


scanner.source = source
let tokens = scanner.scan()
let errors = scanner.errors

tokens.forEach {
if $0.type == TokenType.notEqual {
itHasNotEqual = true
} else if $0.type == TokenType.greaterOrEqual {
itHasGreaterThan = true
}
}

let lastToken = tokens[tokens.index(before: tokens.endIndex)]

XCTAssertEqual(lastToken.line, expectedLines)

XCTAssertTrue(itHasGreaterThan)
XCTAssertTrue(itHasNotEqual)

XCTAssertEqual(tokens.count, 3)
XCTAssertTrue(errors.isEmpty)
}

// line numbers tests
func test_if_it_can_detect_lines_numbers() {
let source =
"""
Expand Down

0 comments on commit 23d0979

Please sign in to comment.