Skip to content

Commit ed07fab

Browse files
committed
Feature - Add single line comment
commit c8034a81ebc16ce8180445500e3de58d48da3373 Author: Þorvaldur Rúnarsson <valdirunars@gmail.com> Date: Thu Sep 27 01:02:06 2018 +0000 Add comment
1 parent c7d0a20 commit ed07fab

File tree

6 files changed

+30
-2
lines changed

6 files changed

+30
-2
lines changed

Sources/ZolangCore/Frontend/Models/Nodes/CodeBlock.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import PathKit
1111

1212
public indirect enum CodeBlock: Node {
1313
case empty
14+
case comment(String)
1415
case expression(Expression)
1516
case variableDeclaration(VariableDeclaration)
1617
case variableMutation(VariableMutation)
@@ -236,6 +237,19 @@ public indirect enum CodeBlock: Node {
236237
let environment = Environment()
237238
let templateString = try String(contentsOf: url, encoding: .utf8)
238239

240+
return try environment.renderTemplate(string: templateString,
241+
context: context)
242+
case .comment(let str):
243+
let context = [
244+
"value": str
245+
]
246+
247+
let url = URL(fileURLWithPath: buildSetting.stencilPath)
248+
.appendingPathComponent("Comment.stencil")
249+
250+
let environment = Environment()
251+
let templateString = try String(contentsOf: url, encoding: .utf8)
252+
239253
return try environment.renderTemplate(string: templateString,
240254
context: context)
241255
}

Sources/ZolangCore/Frontend/Models/Token.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ public enum TokenType: String {
4141

4242
case accessLimitation
4343

44+
case comment
45+
4446
case textLiteral
4547
case booleanLiteral
4648
case floatingPoint
@@ -60,6 +62,7 @@ public struct Token: Equatable {
6062
public static func == (lhs: Token, rhs: Token) -> Bool {
6163
switch (lhs.type, rhs.type) {
6264
case (.parensOpen, .parensOpen),
65+
(.comment, .comment),
6366
(.parensClose, .parensClose),
6467
(.bracketOpen, .bracketOpen),
6568
(.bracketClose, .bracketClose),

Sources/ZolangCore/Frontend/RegExRepo.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ public enum RegExRepo {
3636
public static let `operator` = "or|and|equals|(<=)|(>=)|<|>|plus|minus|times|over"
3737

3838
public static let accessLimitation = "private"
39+
40+
public static let comment = "\\#.*"
3941

4042
public static let boolean = "true|false"
4143
public static let keyword = "describe|make|return|while|from|let|as|be|of|if|else|static"
@@ -46,6 +48,10 @@ extension RegExRepo {
4648
RegExRepo.inlineWhitespaceCharacter: { _ in nil },
4749
RegExRepo.newline: { _ in Token(type: .newline) },
4850

51+
RegExRepo.comment: {
52+
return Token(type: .comment,
53+
payload: String($0.suffix(from: $0.index($0.startIndex, offsetBy: 1))))
54+
},
4955
RegExRepo.accessLimitation: { return Token(type: .accessLimitation, payload: $0) },
5056

5157
RegExRepo.prefixOperator: { return Token(type: .prefixOperator, payload: $0) },

Sources/ZolangCore/Frontend/TokenExtensions/Token+Convenience.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ extension Token {
4747
return Token(type: .textLiteral, payload: text)
4848
}
4949

50+
public static func comment(_ text: String) -> Token {
51+
return Token(type: .comment, payload: text)
52+
}
53+
5054
public static func booleanLiteral(_ value: String) -> Token {
5155
return Token(type: .booleanLiteral, payload: value)
5256
}

Sources/ZolangCore/Frontend/TokenExtensions/Token+Prefix.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ extension Array where Element == Token {
6666
.return,
6767
.operator,
6868
.accessLimitation,
69-
.static:
69+
.static,
70+
.comment:
7071
return false
7172
case .prefixOperator:
7273
guard self.count > 1 else { return false }

Sources/ZolangCore/Frontend/TokenExtensions/Tokens+Helpers.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ extension Array where Element == Token {
244244
.bracketOpen, .bracketClose,
245245
.newline, .describe, .of, .let,
246246
.operator, .other, .accessLimitation,
247-
.static:
247+
.static, .comment:
248248
return nil
249249
}
250250

0 commit comments

Comments
 (0)