Skip to content

Commit

Permalink
Merge 93ebe12 into a0c0da6
Browse files Browse the repository at this point in the history
  • Loading branch information
oxinabox committed Nov 27, 2017
2 parents a0c0da6 + 93ebe12 commit 1e02c74
Show file tree
Hide file tree
Showing 23 changed files with 335 additions and 347 deletions.
25 changes: 19 additions & 6 deletions .travis.yml
@@ -1,16 +1,29 @@
## Documentation: http://docs.travis-ci.com/user/languages/julia/
language: julia
os:
- linux
- osx
julia:
- 0.3
- 0.4
- 0.6
- nightly
notifications:
email: false
# uncomment the following lines to override the default test script
#script:
# - if [[ -a .git/shallow ]]; then git fetch --unshallow; fi
# - julia --check-bounds=yes -e 'Pkg.clone(pwd()); Pkg.build("ParserCombinator"); Pkg.test("ParserCombinator"; coverage=true)'
git:
depth: 99999999

## uncomment the following lines to allow failures on nightly julia
## (tests will run but not make your overall status red)
matrix:
allow_failures:
- julia: nightly

## uncomment and modify the following lines to manually install system packages
#addons:
# apt: # apt-get for linux
# packages:
# - gfortran
#before_script: # homebrew for mac
# - if [ $TRAVIS_OS_NAME = osx ]; then brew install gcc; fi

after_success:
- julia -e 'cd(Pkg.dir("ParserCombinator")); Pkg.add("Coverage"); using Coverage; Coveralls.submit(Coveralls.process_folder())'
18 changes: 6 additions & 12 deletions README.md
Expand Up @@ -36,7 +36,7 @@ using ParserCombinator

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

abstract Node
abstract type Node end
==(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
@@ -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
@@ -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
@@ -1,4 +1,4 @@

__precompile__()
module Parsers

export GML
Expand Down
41 changes: 18 additions & 23 deletions src/core/debug.jl
Expand Up @@ -8,22 +8,20 @@
# 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}<:Config{S}
source::S
stack::Vector
delegate::Config{S,I}
delegate::Config{S}
depth::Vector{Int}
abs_depth::Int
max_depth::Int
max_iter
n_calls::Int
function Debug(source::S; delegate=NoCache, kargs...)
k = delegate{S,I}(source; kargs...)
@compat new(k.source, k.stack, k, Vector{Int}(), 0, 0, start(k.source), 0)
function Debug(source::S; delegate=NoCache, kargs...) where S
k = delegate(source; kargs...)
new{S}(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...)

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

Expand Down Expand Up @@ -73,15 +71,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,17 +91,18 @@ 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


function short(s::Value)
result = string(s)
if ismatch(r"^Any", result)
Expand All @@ -116,12 +111,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 +133,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 +158,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

0 comments on commit 1e02c74

Please sign in to comment.