Skip to content

Commit db16681

Browse files
committed
Reader: more proper comment support.
1 parent eee084f commit db16681

5 files changed

Lines changed: 12 additions & 4 deletions

File tree

Naggum.Test/CompilerTest.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ type CompilerTest() =
3131
static let executableExtension = "exe"
3232

3333
static let directory = @"..\..\..\tests"
34-
static let filenames = [@"test"]
34+
static let filenames = [@"comment"; @"test"]
3535

3636
static member private RunTest testName =
3737
let basePath = Path.Combine(directory, testName)

Naggum.sln

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "ngc", "ngc\ngc.fsproj", "{A
55
EndProject
66
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Test programs", "Test programs", "{2972575D-A421-4759-AE89-57560E0224AE}"
77
ProjectSection(SolutionItems) = preProject
8+
tests\comment.naggum = tests\comment.naggum
9+
tests\comment.result = tests\comment.result
810
tests\test.naggum = tests\test.naggum
911
tests\test.result = tests\test.result
1012
EndProjectSection

ngc/Reader.fs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,9 @@ type SExp =
3535
| Atom of Value
3636
| List of SExp list
3737

38-
let ws parser = parser .>> spaces
39-
let commented parser = ws parser .>> (opt ((skipChar ';') .>> (skipRestOfLine true)))
38+
let comment = (skipChar ';') .>> (skipRestOfLine true)
39+
let skipSpaceAndComments = skipMany (spaces1 <|> comment)
40+
let commented parser = skipSpaceAndComments >>. parser .>> skipSpaceAndComments
4041
let list,listRef = createParserForwardedToRef()
4142

4243
let numberOptions =
@@ -78,7 +79,7 @@ let symbol = (many1Chars (letter <|> digit <|> symChars)) |>> Symbol
7879
let atom = (pnumber <|> string <|> symbol) |>> Atom
7980

8081
let listElement = choice [atom;list]
81-
let sexp = commented (pstring "(") >>. many (commented listElement) .>> commented (pstring ")") |>> List
82+
let sexp = commented (pchar '(') >>. many (commented listElement) .>> commented (pchar ')') |>> List
8283
let parser = many1 (choice [atom;sexp])
8384
do listRef := sexp
8485

tests/comment.naggum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
; a comment
2+
(System.Console.WriteLine "ok")
3+
; a second comment
4+
; a third comment

tests/comment.result

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ok

0 commit comments

Comments
 (0)