Skip to content

Commit

Permalink
Fix to prevent incorrect parsing. All letter-based keywords must now …
Browse files Browse the repository at this point in the history
…be followed by whitespace.
  • Loading branch information
Rickasaurus committed Jun 1, 2016
1 parent 3bbc213 commit a89a2eb
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
16 changes: 11 additions & 5 deletions src/Barb/Parse.fs
Expand Up @@ -274,6 +274,12 @@ let allSimpleMappings =

let whitespaceVocabulary = [" "; "\t"; "\r"; "\n"]

let startsWithAndWhitespace (sw: StringWindow) (token: string) =
if Char.IsLetter(token.[0]) && sw.StartsWith(token) then
whitespaceVocabulary |> List.exists (fun ws -> sw.Subwindow(uint32 token.Length).StartsWith(ws))
elif sw.StartsWith(token) then true
else false

let endUnknownChars =
seq {
for c in whitespaceVocabulary do
Expand Down Expand Up @@ -323,8 +329,8 @@ let (|NewExpression|_|) (typesStack: SubexpressionAndOffset list) (text: StringW
[
for ct in allExpressionTypes do
match ct.Pattern with
| (SCap h) :: rest when text.StartsWith(h) -> yield h, { ct with Pattern = rest }
| (RCap h) :: rest when text.StartsWith(h) -> yield h, ct
| (SCap h) :: rest when startsWithAndWhitespace text h -> yield h, { ct with Pattern = rest }
| (RCap h) :: rest when startsWithAndWhitespace text h -> yield h, ct
| _ -> ()
]
|> List.allMaxBy (fun (m, rest) -> m.Length)
Expand All @@ -340,9 +346,9 @@ let (|NewExpression|_|) (typesStack: SubexpressionAndOffset list) (text: StringW
let (|OngoingExpression|_|) (typesStack: SubexpressionAndOffset list) (text: StringWindow) =
let (|MatchOngoingExpression|_|) (current: SubexpressionType) =
match current.Pattern with
| (SCap h) :: rest when text.StartsWith(h) -> Some (h, { current with Pattern = rest })
| (RCap h) :: rest when text.StartsWith(h) -> Some (h, current)
| (RCap _) :: (SCap h) :: rest when text.StartsWith(h) -> Some (h, { current with Pattern = rest })
| (SCap h) :: rest when startsWithAndWhitespace text h -> Some (h, { current with Pattern = rest })
| (RCap h) :: rest when startsWithAndWhitespace text h -> Some (h, current)
| (RCap _) :: (SCap h) :: rest when startsWithAndWhitespace text h -> Some (h, { current with Pattern = rest })
| _ -> None

match typesStack with
Expand Down
6 changes: 6 additions & 0 deletions tests/Barb.Tests/SyntaxTests.fs
Expand Up @@ -825,6 +825,12 @@ let ``barb should not throw an exception when indexing into a dictionary with a
let res = predicate testRec
Assert.Null(res)

[<Fact>]
let ``barb should terminate a bound lamba at the in after it`` () =
let pred = buildExpr<unit,bool>("let getit = fun info -> if info then info else info in getit true")
let res = pred()
Assert.True(res)

//
// Wish List / Ideas
//
Expand Down

0 comments on commit a89a2eb

Please sign in to comment.