Skip to content

[WIP] Make work in 0.6 #26

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 14 commits into from
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ os:
- linux
- osx
julia:
- 0.3
- 0.4
- 0.5
- nightly
notifications:
email: false
Expand Down
18 changes: 6 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ using ParserCombinator

# the AST nodes we will construct, with evaluation via calc()

abstract Node
abstract type Node node
==(n1::Node, n2::Node) = n1.val == n2.val
calc(n::Float64) = n
type Inv<:Node val end
Expand Down Expand Up @@ -68,7 +68,7 @@ sum.matcher = prd + (add | sub)[0:end] |> Sum
all = sum + Eos()


# and test
# and test

# this prints 2.5
calc(parse_one("1+2*3/4", all)[1])
Expand Down Expand Up @@ -111,11 +111,6 @@ Still, for large parsing tasks (eg parsing source code for a compiler) it
would probably be better to use a wrapper around an external parser generator,
like Anltr.

**Note:** There's an [issue](https://github.com/JuliaLang/Compat.jl/issues/94)
with the Compat library which means the code above (the assignment to
`Delayed.matcher`) doesn't work with 0.3. See [calc.jl](test/calc.jl) for
the uglier, hopefully temporary, 0.3 version.

## Install

```julia
Expand Down Expand Up @@ -596,7 +591,7 @@ character of the first line.

Finally, note that this is implemented at the source level, by restricting
what text is visible to the matchers. Matchers that *could* backtrack will
still make the attempt. So you should also [disable backtracking in the
still make the attempt. So you should also [disable backtracking in the
matchers](#backtracking), where you do not need it, for an efficient grammar.

#### Spaces - Pre And Post-Fixes
Expand Down Expand Up @@ -727,15 +722,15 @@ matchers you care about):

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

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

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

all = sum + Eos()
end

Expand Down Expand Up @@ -1272,4 +1267,3 @@ patch.
1.1.0 - 2015-06-07 - Fixed calc example; debug mode; much rewriting.

1.0.0 - ~2015-06-03 - More or less feature complete.

4 changes: 1 addition & 3 deletions REQUIRE
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
julia 0.3
Compat 0.7.12
julia 0.6
AutoHashEquals 0.0.8

17 changes: 8 additions & 9 deletions src/ParserCombinator.jl
Original file line number Diff line number Diff line change
@@ -1,31 +1,30 @@

__precompile__()
module ParserCombinator

using Compat
using AutoHashEquals
import Base: start, next, done, endof, getindex, colon, isless, size, hash
import Base: ==, ~, +, &, |, >=, >, |>, !

export Matcher,
export Matcher,
diagnostic, forwards, LineSource, LineIter,
Config, Cache, NoCache, make, make_all, make_one, once,
parse_one, parse_one_cache, parse_one_nocache,
parse_one, parse_one_cache, parse_one_nocache,
parse_all, parse_all_cache, parse_all_nocache,
parse_lines, parse_lines_cache,
Debug, Trace,
Debug, Trace,
parse_dbg, parse_one_dbg, parse_one_cache_dbg, parse_one_nocache_dbg,
parse_all_dbg, parse_all_cache_dbg, parse_all_nocache_dbg,
parse_lines_dbg, parse_lines_cache_dbg,
Success, EMPTY, Failure, FAILURE, Execute,
State, Clean, CLEAN, Dirty, DIRTY,
ParserException, Value, Empty, EMPTY, Delegate, DelegateState,
Epsilon, Insert, Dot, Fail, Drop, Equal,
Repeat, Depth, Breadth, Depth!, Breadth!, ALL,
Epsilon, Insert, Dot, Fail, Drop, Equal,
Repeat, Depth, Breadth, Depth!, Breadth!, ALL,
Series, Seq, And, Seq!, And!, Alt, Alt!, Lookahead, Not, Pattern, Delayed, Eos,
ParserError, Error,
Transform, App, Appl, ITransform, IApp, IAppl,
@p_str, @P_str, @e_str, @E_str, Opt, Opt!,
Parse, PUInt, PUInt8, PUInt16, PUInt32, PUInt64,
Parse, PUInt, PUInt8, PUInt16, PUInt32, PUInt64,
PInt, PInt8, PInt16, PInt32, PInt64, PFloat32, PFloat64,
Word, Space,
Star, Plus, Star!, Plus!, StarList, StarList!, PlusList, PlusList!,
Expand All @@ -34,7 +33,7 @@ Star, Plus, Star!, Plus!, StarList, StarList!, PlusList, PlusList!,
TrySource, Try, parse_try, parse_try_dbg, parse_try_cache, parse_try_cache_dbg,
Parsers

FAST_REGEX = isdefined(Main, :FAST_REGEX) ? Main.FAST_REGEX : VERSION >= v"0.4.0-dev+6325"
FAST_REGEX = true

include("core/types.jl")
include("core/sources.jl")
Expand Down
2 changes: 1 addition & 1 deletion src/Parsers.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

__precompile__()
module Parsers

export GML
Expand Down
36 changes: 16 additions & 20 deletions src/core/debug.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
# construct your own with any type by using the delegate=... keyword. see
# test/calc.j for an example.

type Debug{S,I}<:Config{S,I}
mutable struct Debug{S,I}<:Config{S,I}
source::S
stack::Vector
delegate::Config{S,I}
Expand All @@ -17,13 +17,13 @@ type Debug{S,I}<:Config{S,I}
max_depth::Int
max_iter
n_calls::Int
function Debug(source::S; delegate=NoCache, kargs...)
function Debug(source::S; delegate=NoCache, kargs...) where S
k = delegate{S,I}(source; kargs...)
@compat new(k.source, k.stack, k, Vector{Int}(), 0, 0, start(k.source), 0)
new{S,I}(k.source, k.stack, k, Vector{Int}(), 0, 0, start(k.source), 0)
end
end
# i don't get why this is necessary, but it seems to work
Debug(source; kargs...) = Debug{typeof(source),typeof(start(source))}(source; kargs...)
#Debug(source; kargs...) = Debug{typeof(source),typeof(start(source))}(source; kargs...)

parent(k::Debug) = parent(k.delegate)

Expand Down Expand Up @@ -73,15 +73,11 @@ MAX_RES = 50
MAX_SRC = 10
MAX_IND = 10

if VERSION < v"0.4-"
shorten(s) = s
else
# shorten(s) = replace(s, r"(?:[a-zA-Z]+\.)+([a-zA-Z]+)", s"\1")
shorten(s) = replace(s, r"(?:[a-zA-Z]+\.)+([a-zA-Z]+)",
Base.SubstitutionString("\1"))
end
shorten(s) = replace(s, r"(?:[a-zA-Z]+\.)+([a-zA-Z]+)",
Base.SubstitutionString("\1"))

function truncate(s::AbstractString, n=10)
function truncate(s::String, n=10)
if length(s) <= n
return s
end
Expand All @@ -97,13 +93,13 @@ function truncate(s::AbstractString, n=10)
end
end

pad(s::AbstractString, n::Int) = s * repeat(" ", n - length(s))
pad(s::String, n::Int) = s * repeat(" ", n - length(s))
indent(k::Debug; max=MAX_IND) = repeat(" ", k.depth[end] % max)

src(::Any, ::Any; max=MAX_SRC) = pad(truncate("...", max), max)
src(s::AbstractString, i::Int; max=MAX_SRC) = pad(truncate(escape_string(s[i:end]), max), max)
src(s::String, i::Int; max=MAX_SRC) = pad(truncate(escape_string(s[i:end]), max), max)

function debug{S<:AbstractString}(k::Debug{S}, e::Execute)
function debug(k::Debug{<:AbstractString}, e::Execute)
@printf("%3d:%s %02d %s%s->%s\n",
e.iter, src(k.source, e.iter), k.depth[end], indent(k), e.parent.name, e.child.name)
end
Expand All @@ -116,12 +112,12 @@ function short(s::Value)
truncate(result, MAX_RES)
end

function debug{S<:AbstractString}(k::Debug{S}, s::Success)
function debug(k::Debug{<:AbstractString}, s::Success)
@printf("%3d:%s %02d %s%s<-%s\n",
s.iter, src(k.source, s.iter), k.depth[end], indent(k), parent(k).name, short(s.result))
end

function debug{S<:AbstractString}(k::Debug{S}, f::Failure)
function debug(k::Debug{<:AbstractString}, f::Failure)
@printf(" :%s %02d %s%s<-!!!\n",
pad(" ", MAX_SRC), k.depth[end], indent(k), parent(k).name)
end
Expand All @@ -138,17 +134,17 @@ function src(s::LineAt, i::LineIter; max=MAX_SRC)
end
end

function debug{S<:LineAt}(k::Debug{S}, e::Execute)
function debug(k::Debug{<:LineAt}, e::Execute)
@printf("%3d,%-3d:%s %02d %s%s->%s\n",
e.iter.line, e.iter.column, src(k.source, e.iter), k.depth[end], indent(k), e.parent.name, e.child.name)
end

function debug{S<:LineAt}(k::Debug{S}, s::Success)
function debug(k::Debug{<:LineAt}, s::Success)
@printf("%3d,%-3d:%s %02d %s%s<-%s\n",
s.iter.line, s.iter.column, src(k.source, s.iter), k.depth[end], indent(k), parent(k).name, short(s.result))
end

function debug{S<:LineAt}(k::Debug{S}, f::Failure)
function debug(k::Debug{<:LineAt}, f::Failure)
@printf(" :%s %02d %s%s<-!!!\n",
pad(" ", MAX_SRC), k.depth[end], indent(k), parent(k).name)
end
Expand All @@ -163,7 +159,7 @@ end
Trace(matcher) = new(:Trace, matcher)
end

@auto_hash_equals immutable TraceState<:DelegateState
@auto_hash_equals struct TraceState<:DelegateState
state::State
end

Expand Down
Loading