Skip to content

Commit

Permalink
replace s for e
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewcooke committed Jul 26, 2015
1 parent b22d778 commit 030df48
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 65 deletions.
68 changes: 34 additions & 34 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,17 @@ calc(s::Sum) = Base.sum(map(calc, s.val))
# the grammar (the combinators!)

sum = Delayed()
val = S"(" + sum + S")" | PFloat64()
val = E"(" + sum + E")" | PFloat64()

neg = Delayed() # allow multiple (or no) negations (eg ---3)
neg.matcher = val | (S"-" + neg > Neg)
neg.matcher = val | (E"-" + neg > Neg)

mul = S"*" + neg
div = S"/" + neg > Inv
mul = E"*" + neg
div = E"/" + neg > Inv
prd = neg + (mul | div)[0:end] |> Prd

add = S"+" + prd
sub = S"-" + prd > Neg
add = E"+" + prd
sub = E"-" + prd > Neg
sum.matcher = prd + (add | sub)[0:end] |> Sum

all = sum + Eos()
Expand All @@ -81,7 +81,7 @@ Some explanation of the above:
"parser combinator" style if you prefer. For example, `Seq(...)` instead of
`+`, or `App(...)` instead of `>`.

* The matcher `S"xyz"` matches and then discards the string `"xyz"`.
* The matcher `E"xyz"` matches and then discards the string `"xyz"`.

* Every matcher returns a list of matched values. This can be an empty list
if the match succeeded but matched nothing.
Expand Down Expand Up @@ -190,12 +190,12 @@ julia> parse_one("abc", Equal("abx"))
ERROR: ParserCombinator.ParserException("cannot parse")
```

This is so common that there's a corresponding [string
literal](http://julia.readthedocs.org/en/latest/manual/strings/#non-standard-string-literals)
(it's "s" for "string").
This is so common that there's a corresponding
[string literal](http://julia.readthedocs.org/en/latest/manual/strings/#non-standard-string-literals)
(it's "e" for `Equal(), the corresponding matcher).

```julia
julia> parse_one("abc", s"ab")
julia> parse_one("abc", e"ab")
1-element Array{Any,1}:
"ab"
```
Expand Down Expand Up @@ -226,12 +226,12 @@ julia> parse_one("abc", And(Equal("a"), Equal("b")))
Any["a"]
Any["b"]

julia> parse_one("abc", s"a" + s"b")
julia> parse_one("abc", e"a" + e"b")
2-element Array{Any,1}:
"a"
"b"

julia> parse_one("abc", s"a" & s"b")
julia> parse_one("abc", e"a" & e"b")
2-element Array{Any,1}:
Any["a"]
Any["b"]
Expand Down Expand Up @@ -275,11 +275,11 @@ julia> parse_one("abc", Seq(Drop(Equal("a")), Equal("b")))
1-element Array{Any,1}:
"b"

julia> parse_one("abc", ~s"a" + s"b")
julia> parse_one("abc", ~e"a" + e"b")
1-element Array{Any,1}:
"b"

julia> parse_one("abc", S"a" + s"b")
julia> parse_one("abc", E"a" + e"b")
1-element Array{Any,1}:
"b"
```
Expand All @@ -290,11 +290,11 @@ examples, respectively.
#### Alternates

```julia
julia> parse_one("abc", Alt(s"x", s"a"))
julia> parse_one("abc", Alt(e"x", e"a"))
1-element Array{Any,1}:
"a"

julia> parse_one("abc", s"x" | s"a")
julia> parse_one("abc", e"x" | e"a")
1-element Array{Any,1}:
"a"
```
Expand Down Expand Up @@ -531,12 +531,12 @@ theory, the following two grammars have different backtracking
behaviour:

```julia
Series(Repeat(s"a", 0, 3), s"b"; backtrack=false)
Series(Repeat(s"a", 0, 3; backtrack=false), s"b"; backtrack=false)
Series(Repeat(e"a", 0, 3), e"b"; backtrack=false)
Series(Repeat(e"a", 0, 3; backtrack=false), e"b"; backtrack=false)
```

(although, in practice, they are identical, in this contrived example,
because `s"a"` doesn't backtrack anyway).
because `e"a"` doesn't backtrack anyway).

This makes a grammar more efficient, but also more specific. It can
reduce the memory consumed by the parser, but does not guarantee that
Expand Down Expand Up @@ -566,12 +566,12 @@ abcdefghijklmnopqrstuvwxyz
```julia
open("test1.txt", "r") do io
# this throws an execption because it requires backtracking
parse_try(io, p"[a-z]"[0:end] + s"m" > string)
parse_try(io, p"[a-z]"[0:end] + e"m" > string)
end

open("test1.txt", "r") do io
# this (with Try(...)) works fine
parse_try(io, Try(p"[a-z]"[0:end] + s"m" > string))
parse_try(io, Try(p"[a-z]"[0:end] + e"m" > string))
end
```

Expand Down Expand Up @@ -607,17 +607,17 @@ spc = Drop(Star(Space()))
@with_pre spc begin

sum = Delayed()
val = S"(" + spc + sum + spc + S")" | PFloat64()
val = E"(" + spc + sum + spc + E")" | PFloat64()

neg = Delayed() # allow multiple negations (eg ---3)
neg.matcher = Nullable{Matcher}(val | (S"-" + neg > Neg))
neg.matcher = Nullable{Matcher}(val | (E"-" + neg > Neg))

mul = S"*" + neg
div = S"/" + neg > Inv
mul = E"*" + neg
div = E"/" + neg > Inv
prd = neg + (mul | div)[0:end] |> Prd

add = S"+" + prd
sub = S"-" + prd > Neg
add = E"+" + prd
sub = E"-" + prd > Neg
sum.matcher = Nullable{Matcher}(prd + (add | sub)[0:end] |> Sum)

all = sum + spc + Eos()
Expand Down Expand Up @@ -709,17 +709,17 @@ matchers you care about):
@with_names begin

sum = Delayed()
val = S"(" + sum + S")" | PFloat64()
val = E"(" + sum + E")" | PFloat64()

neg = Delayed() # allow multiple negations (eg ---3)
neg.matcher = val | (S"-" + neg > Neg)
neg.matcher = val | (E"-" + neg > Neg)

mul = S"*" + neg
div = S"/" + neg > Inv
mul = E"*" + neg
div = E"/" + neg > Inv
prd = neg + (mul | div)[0:end] |> Prd

add = S"+" + prd
sub = S"-" + prd > Neg
add = E"+" + prd
sub = E"-" + prd > Neg
sum.matcher = prd + (add | sub)[0:end] |> Sum

all = sum + Eos()
Expand Down
2 changes: 1 addition & 1 deletion src/ParserCombinator.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Repeat, Depth, Breadth, Depth!, Breadth!, ALL,
Series, Seq, And, Seq!, And!, Alt, Alt!, Lookahead, Not, Pattern, Delayed, Eos,
ParserError, Error,
Transform, App, Appl,
@p_str, @P_str, @s_str, @S_str, Opt,
@p_str, @P_str, @e_str, @E_str, Opt,
Parse, PUInt, PUInt8, PUInt16, PUInt32, PUInt64,
PInt, PInt8, PInt16, PInt32, PInt64, PFloat32, PFloat64,
Word, Space, Star, Plus,
Expand Down
6 changes: 3 additions & 3 deletions src/core/sugar.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ macro P_str(s)
Drop(Pattern(Regex(s)))
end

# s"..." creates a matcher for the given string
macro s_str(s)
# e"..." creates a matcher for the given string
macro e_str(s)
Equal(s)
end

macro S_str(s)
macro E_str(s)
Drop(Equal(s))
end

Expand Down
4 changes: 2 additions & 2 deletions src/gml/GML.jl
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ function mk_parser()
key = p"[a-zA-Z][a-zA-Z0-9]*" > symbol
int = p"(\+|-)?\d+" > parse_int
real = p"(\+|-)?\d+.\d+((E|e)(\+|-)?\d+)?" > parse_flt
str = Seq!(S"\"", p"[^\"]+"[0:end,:!], S"\"") > string
str = Seq!(E"\"", p"[^\"]+"[0:end,:!], E"\"") > string

list = Delayed()
sublist = Seq!(S"[", spc, list, Alt!(Seq!(S"]", spc), expect("]")))
sublist = Seq!(E"[", spc, list, Alt!(Seq!(E"]", spc), expect("]")))
value = Seq!(Alt!(real, int, str, sublist, expect("value")), spc)
element = Seq!(key, space, value) > tuple

Expand Down
12 changes: 6 additions & 6 deletions test/core/calc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@ calc(s::Sum) = signed_sum(map(calc, s.val))
@with_names begin

sum = Delayed()
val = S"(" + sum + S")" | PFloat64()
val = E"(" + sum + E")" | PFloat64()

neg = Delayed() # allow multiple negations (eg ---3)
neg.matcher = Nullable{Matcher}(val | (S"-" + neg > Neg))
neg.matcher = Nullable{Matcher}(val | (E"-" + neg > Neg))

mul = S"*" + neg
div = S"/" + neg > Inv
mul = E"*" + neg
div = E"/" + neg > Inv
prd = neg + (mul | div)[0:end] |> Prd

add = S"+" + prd
sub = S"-" + prd > Neg
add = E"+" + prd
sub = E"-" + prd > Neg
sum.matcher = Nullable{Matcher}(prd + (add | sub)[0:end] |> Sum)

all = sum + Eos()
Expand Down
12 changes: 6 additions & 6 deletions test/core/fix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,17 @@ calc(s::Sum) = signed_sum(map(calc, s.val))
@with_pre spc begin

sum = Delayed()
val = S"(" + spc + sum + spc + S")" | PFloat64()
val = E"(" + spc + sum + spc + E")" | PFloat64()

neg = Delayed() # allow multiple negations (eg ---3)
neg.matcher = Nullable{Matcher}(val | (S"-" + neg > Neg))
neg.matcher = Nullable{Matcher}(val | (E"-" + neg > Neg))

mul = S"*" + neg
div = S"/" + neg > Inv
mul = E"*" + neg
div = E"/" + neg > Inv
prd = neg + (mul | div)[0:end] |> Prd

add = S"+" + prd
sub = S"-" + prd > Neg
add = E"+" + prd
sub = E"-" + prd > Neg
sum.matcher = Nullable{Matcher}(prd + (add | sub)[0:end] |> Sum)

all = sum + spc + Eos()
Expand Down
2 changes: 1 addition & 1 deletion test/core/print.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

println(Seq())
println(s"a" + S"b" | p"c" > tuple)
println(e"a" + E"b" | p"c" > tuple)

println("print ok")
16 changes: 8 additions & 8 deletions test/core/tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@
@test parse_one("abc", Seq(p"."[1:2,:&,:?], p"."[1:2])) == Any[["a"], "b", "c"]
@test_throws ErrorException parse_one("abc", Seq(p"."[1:2,:&,:?,:x], p"."[1:2]))
@test parse_one("abc", Seq(p"."[1:2], p"."[1:2], Equal("c"))) == ["a", "b", "c"]
@test parse_one("ab", p"." + s"b") == ["a", "b"]
@test parse_one("abc", p"." + s"b" + s"c") == ["a", "b", "c"]
@test parse_one("abc", p"." + S"b" + s"c") == ["a", "c"]
@test parse_one("b", Alt(s"a", s"b", s"c")) == ["b"]
@test parse_one("b", Alt!(s"a", s"b", s"c")) == ["b"]
@test collect(parse_all("b", Trace(Alt(Epsilon(), Repeat(s"b", 0, 1))))) == Array[[], ["b"], []]
@test collect(parse_all("b", Alt(Epsilon(), Repeat(s"b", 0, 1; greedy=false)))) == Array[[], [], ["b"]]
@test parse_one("abc", p"." + (s"b" | s"c")) == ["a", "b"]
@test parse_one("ab", p"." + e"b") == ["a", "b"]
@test parse_one("abc", p"." + e"b" + e"c") == ["a", "b", "c"]
@test parse_one("abc", p"." + E"b" + e"c") == ["a", "c"]
@test parse_one("b", Alt(e"a", e"b", e"c")) == ["b"]
@test parse_one("b", Alt!(e"a", e"b", e"c")) == ["b"]
@test collect(parse_all("b", Trace(Alt(Epsilon(), Repeat(e"b", 0, 1))))) == Array[[], ["b"], []]
@test collect(parse_all("b", Alt(Epsilon(), Repeat(e"b", 0, 1; greedy=false)))) == Array[[], [], ["b"]]
@test parse_one("abc", p"." + (e"b" | e"c")) == ["a", "b"]
@test length(collect(parse_all("abc", p"."[0:3]))) == 4
@test length(collect(parse_all("abc", p"."[1:2]))) == 2
@test parse_one("abc", p"."[3] > tuple) == [("a", "b", "c")]
Expand Down
8 changes: 4 additions & 4 deletions test/core/try.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,24 @@ open("core/test1.txt", "r") do io
end

#open("test1.txt", "r") do io
# parse_one_dbg(TrySource(io), Trace(p"[a-z]"[0:end] + s"m" > string); debug=true)
# parse_one_dbg(TrySource(io), Trace(p"[a-z]"[0:end] + e"m" > string); debug=true)
#end

for parse in (parse_try, parse_try_cache, parse_try_dbg, parse_try_cache_dbg)

open("core/test1.txt", "r") do io
@test_throws ParserException parse(io, Trace(p"[a-z]"[0:end] + s"m" > string))
@test_throws ParserException parse(io, Trace(p"[a-z]"[0:end] + e"m" > string))
end

open("core/test1.txt", "r") do io
result = parse(io, Try(p"[a-z]"[0:end] + s"m" > string))
result = parse(io, Try(p"[a-z]"[0:end] + e"m" > string))
println(result)
@test result == Any["abcdefghijklm"]
end

open("core/test1.txt", "r") do io
# multiple lines
result = parse(io, Try(p"(.|\n)"[0:end] + s"5" > string))
result = parse(io, Try(p"(.|\n)"[0:end] + e"5" > string))
println(result)
@test result == Any["abcdefghijklmnopqrstuvwxyz\n012345"]
end
Expand Down
3 changes: 3 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ using ParserCombinator
using Base.Test
using Compat

#include("gml/10k-49963.jl")
#exit(0)

include("core/sources.jl")
include("core/fix.jl")
include("core/print.jl")
Expand Down

0 comments on commit 030df48

Please sign in to comment.