From 23d0979c99a7e8caddaf55fd2be263fbd80dad38 Mon Sep 17 00:00:00 2001 From: Anas mostefaoui Date: Wed, 7 Mar 2018 16:29:45 +0300 Subject: [PATCH] Add ignoring white space, tab(\t) carriage return (\r) and new line (\n) characters --- LoxInterpreter/LoxScanner.swift | 18 ++- .../ScannerTests/ScannerUnitTests.swift | 135 +++++++++++++++++- 2 files changed, 150 insertions(+), 3 deletions(-) diff --git a/LoxInterpreter/LoxScanner.swift b/LoxInterpreter/LoxScanner.swift index d0a4ac3..59db018 100644 --- a/LoxInterpreter/LoxScanner.swift +++ b/LoxInterpreter/LoxScanner.swift @@ -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() @@ -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 } @@ -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) diff --git a/LoxInterpreterTests/UnitTests/ScannerTests/ScannerUnitTests.swift b/LoxInterpreterTests/UnitTests/ScannerTests/ScannerUnitTests.swift index 573ca53..249d98e 100644 --- a/LoxInterpreterTests/UnitTests/ScannerTests/ScannerUnitTests.swift +++ b/LoxInterpreterTests/UnitTests/ScannerTests/ScannerUnitTests.swift @@ -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 @@ -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 @@ -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 = """