Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Support to newline as expressions delimiter
  • Loading branch information
ElemarJR committed Oct 29, 2015
1 parent c23dc28 commit f4c4701
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 92 deletions.
5 changes: 4 additions & 1 deletion demo/bellevue/Bellevue.Parser/Script.fsx
Expand Up @@ -6,5 +6,8 @@ open Bellevue.Parser

// Define your library scripting code here

TextParser.Parse("sample @{a=10;a+2} @a")
TextParser.Parse("@{
c = a
d = 7
}")

13 changes: 10 additions & 3 deletions demo/bellevue/Bellevue.Parser/parser.fs
Expand Up @@ -70,6 +70,13 @@ module private Util =
| _ -> None
loop chars

let (|Ignorables|) chars =
let rec loop = function
| ' '::xs -> (loop xs)
| '\t'::xs -> (loop xs)
| xs -> xs
loop chars

let rec parse acc chars = seq {
let emitLiteral() = seq {
if acc <> [] then
Expand All @@ -85,10 +92,10 @@ module private Util =
let (|Instruction|_|) chars =
let rec loop acc chars =
match chars with
| x::xs when x = ';' ->
| x::xs when x = ';' || x = '\n' ->
if acc <> []
then Some((acc |> List.rev), xs)
else None
else loop [] xs
| x::xs -> loop (x::acc) xs
| [] ->
if acc <> []
Expand All @@ -100,7 +107,7 @@ module private Util =
match chars with
| Instruction(instruction, body) ->
match instruction with
| Id(variable, Equals(value)) ->
| Ignorables(Id(variable, Equals(value))) ->
yield Assignment(variable, value |> toString)
| _ -> yield! emitFormula instruction

Expand Down
5 changes: 5 additions & 0 deletions demo/bellevue/Bellevue/sample.txt
Expand Up @@ -15,5 +15,10 @@ This source text @*this is a comment*@will be compiled into an executable.
Then "a" is incremented @{a=a+1}
Value of a is @a
a + 2 : @{a + 2}
@{
c = a
d = 7
}
c, d : @c, @d

Bye!
176 changes: 88 additions & 88 deletions src/FluentIL/ExpressionParser/ExpressionScanner.cs
@@ -1,89 +1,89 @@
namespace FluentIL.ExpressionParser
{
internal class ExpressionScanner : Scanner
{
public ExpressionScanner() :
base(BuildTable())
{
}

private static StateTable BuildTable()
{
const string whitespaces = " \t\n";
const string digits = "0123456789";
const string letters = "qwertyuiopasdfghjklzxcvbnm";

return new StateTable("s1")
.WithState("s1", new State()
.WithGoTo(whitespaces, "s17")
.WithGoTo(digits, "s14")
.WithGoTo(letters, "s16")
.WithGoTo('/', "s2")
.WithGoTo('.', "s13")
)
.WithState("s2", new State("divide")
.WithGoTo('/', "s3")
.WithGoTo('*', "s4")
)
.WithState("s3", new State()
.WithGoTo(" \t", "s3")
.WithGoTo('\n', "s18")
.WithGoTo("/*()+-:.", "s3")
.WithGoTo(digits, "s3")
.WithGoTo(letters, "s3")
)
.WithState("s4", new State()
.WithGoTo(" \t\n/()+-:.", "s4")
.WithGoTo('*', "s5")
.WithGoTo(digits, "s4")
.WithGoTo(letters, "s4")
)
.WithState("s5", new State()
.WithGoTo(whitespaces, "s4")
.WithGoTo('/', "s18")
.WithGoTo('*', "s5")
.WithGoTo("()+-:.", "s4")
.WithGoTo(digits, "s4")
.WithGoTo(letters, "s4")
)
.WithState("s12", new State("assign"))
.WithState("s13", new State()
.WithGoTo(digits, "s15")
)
.WithState("s14", new State("integer")
.WithGoTo(digits, "s14")
.WithGoTo('.', "s15")
)
.WithState("s15", new State("float")
.WithGoTo(digits, "s15")
)
.WithState("s16", new State("identifier")
.WithGoTo(digits, "s16")
.WithGoTo(letters, "s16")
)
.WithState("s17", new State("white_space")
.WithGoTo(whitespaces, "s17")
)
.WithState("s18", new State("comment"))
.WithToken('(', "lparen")
.WithToken(')', "rparen")
.WithToken('+', "plus")
.WithToken('-', "minus")
.WithToken('*', "times")
.WithToken('>', "gt")
.WithToken(">=", "geq")
.WithToken('<', "lt")
.WithToken("<=", "leq")
.WithToken("==", "eq")
.WithToken("!", "not")
.WithToken("!=", "neq")
.WithToken("<>", "neq")
.WithToken(":=", "assign")
.WithToken("||", "or")
.WithToken("&&", "and")
.WithToken("^", "pow")
.WithToken("%", "mod")
;
}
}
namespace FluentIL.ExpressionParser
{
internal class ExpressionScanner : Scanner
{
public ExpressionScanner() :
base(BuildTable())
{
}

private static StateTable BuildTable()
{
const string whitespaces = " \t\n\r";
const string digits = "0123456789";
const string letters = "qwertyuiopasdfghjklzxcvbnm";

return new StateTable("s1")
.WithState("s1", new State()
.WithGoTo(whitespaces, "s17")
.WithGoTo(digits, "s14")
.WithGoTo(letters, "s16")
.WithGoTo('/', "s2")
.WithGoTo('.', "s13")
)
.WithState("s2", new State("divide")
.WithGoTo('/', "s3")
.WithGoTo('*', "s4")
)
.WithState("s3", new State()
.WithGoTo(" \t", "s3")
.WithGoTo('\n', "s18")
.WithGoTo("/*()+-:.", "s3")
.WithGoTo(digits, "s3")
.WithGoTo(letters, "s3")
)
.WithState("s4", new State()
.WithGoTo(" \t\n/()+-:.", "s4")
.WithGoTo('*', "s5")
.WithGoTo(digits, "s4")
.WithGoTo(letters, "s4")
)
.WithState("s5", new State()
.WithGoTo(whitespaces, "s4")
.WithGoTo('/', "s18")
.WithGoTo('*', "s5")
.WithGoTo("()+-:.", "s4")
.WithGoTo(digits, "s4")
.WithGoTo(letters, "s4")
)
.WithState("s12", new State("assign"))
.WithState("s13", new State()
.WithGoTo(digits, "s15")
)
.WithState("s14", new State("integer")
.WithGoTo(digits, "s14")
.WithGoTo('.', "s15")
)
.WithState("s15", new State("float")
.WithGoTo(digits, "s15")
)
.WithState("s16", new State("identifier")
.WithGoTo(digits, "s16")
.WithGoTo(letters, "s16")
)
.WithState("s17", new State("white_space")
.WithGoTo(whitespaces, "s17")
)
.WithState("s18", new State("comment"))
.WithToken('(', "lparen")
.WithToken(')', "rparen")
.WithToken('+', "plus")
.WithToken('-', "minus")
.WithToken('*', "times")
.WithToken('>', "gt")
.WithToken(">=", "geq")
.WithToken('<', "lt")
.WithToken("<=", "leq")
.WithToken("==", "eq")
.WithToken("!", "not")
.WithToken("!=", "neq")
.WithToken("<>", "neq")
.WithToken(":=", "assign")
.WithToken("||", "or")
.WithToken("&&", "and")
.WithToken("^", "pow")
.WithToken("%", "mod")
;
}
}
}

0 comments on commit f4c4701

Please sign in to comment.