-
Notifications
You must be signed in to change notification settings - Fork 225
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Project] Use 2 spaces for indentation
- Loading branch information
Showing
20 changed files
with
1,097 additions
and
1,095 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,57 @@ | ||
import Foundation | ||
|
||
public struct Lexer { | ||
public let templateString:String | ||
let regex = NSRegularExpression(pattern: "(\\{\\{.*?\\}\\}|\\{%.*?%\\}|\\{#.*?#\\})", options: nil, error: nil)! | ||
public let templateString:String | ||
let regex = NSRegularExpression(pattern: "(\\{\\{.*?\\}\\}|\\{%.*?%\\}|\\{#.*?#\\})", options: nil, error: nil)! | ||
|
||
public init(templateString:String) { | ||
self.templateString = templateString | ||
} | ||
|
||
func createToken(string:String) -> Token { | ||
func strip() -> String { | ||
return string[string.startIndex.successor().successor()..<string.endIndex.predecessor().predecessor()].stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceCharacterSet()) | ||
} | ||
public init(templateString:String) { | ||
self.templateString = templateString | ||
} | ||
|
||
if string.hasPrefix("{{") { | ||
return Token.Variable(value: strip()) | ||
} else if string.hasPrefix("{%") { | ||
return Token.Block(value: strip()) | ||
} else if string.hasPrefix("{#") { | ||
return Token.Comment(value: strip()) | ||
} | ||
func createToken(string:String) -> Token { | ||
func strip() -> String { | ||
return string[string.startIndex.successor().successor()..<string.endIndex.predecessor().predecessor()].stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceCharacterSet()) | ||
} | ||
|
||
return Token.Text(value: string) | ||
if string.hasPrefix("{{") { | ||
return Token.Variable(value: strip()) | ||
} else if string.hasPrefix("{%") { | ||
return Token.Block(value: strip()) | ||
} else if string.hasPrefix("{#") { | ||
return Token.Comment(value: strip()) | ||
} | ||
|
||
/// Returns an array of tokens from a given template string. | ||
public func tokenize() -> [Token] { | ||
// Unfortunately NSRegularExpression doesn't have a split. | ||
// So here's a really terrible implementation | ||
return Token.Text(value: string) | ||
} | ||
|
||
var tokens = [Token]() | ||
/// Returns an array of tokens from a given template string. | ||
public func tokenize() -> [Token] { | ||
// Unfortunately NSRegularExpression doesn't have a split. | ||
// So here's a really terrible implementation | ||
|
||
let range = NSMakeRange(0, count(templateString)) | ||
var lastIndex = 0 | ||
let nsTemplateString = templateString as NSString | ||
let options = NSMatchingOptions(0) | ||
regex.enumerateMatchesInString(templateString, options: options, range: range) { (result, flags, b) in | ||
if result.range.location != lastIndex { | ||
let previousMatch = nsTemplateString.substringWithRange(NSMakeRange(lastIndex, result.range.location - lastIndex)) | ||
tokens.append(self.createToken(previousMatch)) | ||
} | ||
var tokens = [Token]() | ||
|
||
let match = nsTemplateString.substringWithRange(result.range) | ||
tokens.append(self.createToken(match)) | ||
let range = NSMakeRange(0, count(templateString)) | ||
var lastIndex = 0 | ||
let nsTemplateString = templateString as NSString | ||
let options = NSMatchingOptions(0) | ||
regex.enumerateMatchesInString(templateString, options: options, range: range) { (result, flags, b) in | ||
if result.range.location != lastIndex { | ||
let previousMatch = nsTemplateString.substringWithRange(NSMakeRange(lastIndex, result.range.location - lastIndex)) | ||
tokens.append(self.createToken(previousMatch)) | ||
} | ||
|
||
lastIndex = result.range.location + result.range.length | ||
} | ||
let match = nsTemplateString.substringWithRange(result.range) | ||
tokens.append(self.createToken(match)) | ||
|
||
if lastIndex < count(templateString) { | ||
let substring = (templateString as NSString).substringFromIndex(lastIndex) | ||
tokens.append(Token.Text(value: substring)) | ||
} | ||
lastIndex = result.range.location + result.range.length | ||
} | ||
|
||
return tokens | ||
if lastIndex < count(templateString) { | ||
let substring = (templateString as NSString).substringFromIndex(lastIndex) | ||
tokens.append(Token.Text(value: substring)) | ||
} | ||
|
||
return tokens | ||
} | ||
} |
Oops, something went wrong.