Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/.deps/
/libs/
/lib
.crystal/


Expand Down
14 changes: 6 additions & 8 deletions examples/exp_lang/spec/xparser_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,20 @@ require "../xparser"
lexer = EXP_LANG::Lexer
parser = EXP_LANG::Parser

it "EXP_LANG::Parser::Comments" do
describe "EXP_LANG::Parser::Comments" do
it "parses a program ignoring commented lines" do
string = "1 + 2 # simple addition \n" \
"# one long comment spanning \n" \
"# several lines \n"
tokens = lexer.lex(string)

string = "1 + 2 # simple addition \n" \
"# one long comment spanning \n" \
"# several lines \n"
tokens = lexer.lex(string)

it "parses a programm ignoring commented lines" do
res = parser.parse(tokens, {accept: :first}).as(XProgram).expressions.as(Array).first
res.class.should eq Add
rright = res.as(Add).right.as(ANumber)
rleft = res.as(Add).left.as(ANumber)
rleft.value.should eq 1.to_f
rright.value.should eq 2.to_f
end

end

describe "EXP_LANG::Parser::VariableAssignment" do
Expand Down
15 changes: 8 additions & 7 deletions examples/exp_lang/spec/xscope_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,20 @@ describe "EXP_LANG::Scope" do
end

describe "#clone" do
scope = EXP_LANG::Scope(String).new
scope["name"] = "first"
scope["age"] = "twelve"
original_scope = EXP_LANG::Scope(String).new
original_scope["name"] = "first"
original_scope["age"] = "twelve"

it "can clone itself" do
scope.clone.should_not eq scope
cloned_scope = original_scope.clone
cloned_scope.should_not eq original_scope
end

it "will clone all the values" do
scope.clone["name"].should eq "first"
scope.clone["age"].should eq "twelve"
cloned_scope = original_scope.clone
cloned_scope["name"].should eq "first"
cloned_scope["age"].should eq "twelve"
end

end
end
context "recursive lookups" do
Expand Down
2 changes: 1 addition & 1 deletion shard.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ authors:

dependencies:
crystal-dfa:
github: ziprandom/crystal-dfa
github: dsisnero/crystal-dfa
branch: master
msgpack:
github: crystal-community/msgpack-crystal
Expand Down
51 changes: 24 additions & 27 deletions spec/cltk/lexer_spec.cr
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
require "../spec_helper"

class ENVLexer < CLTK::Lexer

class Environment < CLTK::Lexer::Environment

def initialize(*args)
@value = -1
super(*args)
Expand All @@ -13,41 +11,39 @@ class ENVLexer < CLTK::Lexer
@value += 1
@value.as(Int32)
end

end

rule(/a/) do |txt|
next {:A, next_value }
next {:A, next_value}
end
end

class ABLongest < CLTK::Lexer
rule(/a+/) { :APLUS }
rule(/b+/) { :BPLUS }
rule(/a+/) { :APLUS }
rule(/b+/) { :BPLUS }

rule(/a+b+/) { :APLUSBPLUS }
end

class ABFirst < CLTK::Lexer
match_first

rule(/a+/) { :APLUS }
rule(/b+/) { :BPLUS }
rule(/a+/) { :APLUS }
rule(/b+/) { :BPLUS }

rule(/a+b+/) { :APLUSBPLUS }
end


class FlagLexer < CLTK::Lexer
rule(/a/) { |txt| set_flag(:a); :A }
rule(/a/) { |txt| set_flag(:a); :A }
rule(/\s/)

rule(/b/, :default, [:a]) { |txt| set_flag(:b); :B }
rule(/b/, :default, [:a]) { |txt| set_flag(:b); :B }
rule(/c/, :default, [:a, :b]) { :C }
end

class StateLexer < CLTK::Lexer
rule(/a/) { :A }
rule(/a/) { :A }
rule(/\s/)

rule(/\(\*/) { |txt|
Expand All @@ -56,17 +52,20 @@ class StateLexer < CLTK::Lexer

rule(/\(\*/, :comment) { |txt| push_state(:comment) }
rule(/\*\)/, :comment) { |txt| pop_state }
rule(/./, :comment)
rule(/./, :comment)
end

class MatchDataLexer < CLTK::Lexer
rule(/a(b*)(c+)/) do |txt|
{:FOO, [match[1]?, match[2]?].join(", ") }
if matched = match
{:FOO, [matched[1]?, matched[2]?].join(", ")}
else
{:FOO, ""}
end
end
end

describe "CLTK::Lexer" do

describe "test_calc" do
expected = [
CLTK::Token.new(:NUM, 1),
Expand All @@ -78,7 +77,7 @@ describe "CLTK::Lexer" do

CLTK::Token.new(:LPAREN),
CLTK::Token.new(:RPAREN),
CLTK::Token.new(:EOS)
CLTK::Token.new(:EOS),
]
actual = CLTK::Lexers::Calculator.lex("1 + - * / ( )")
expected.should eq actual
Expand All @@ -92,7 +91,7 @@ describe "CLTK::Lexer" do
CLTK::Token.new(:STAR),
CLTK::Token.new(:PLUS),
CLTK::Token.new(:QUESTION),
CLTK::Token.new(:EOS)
CLTK::Token.new(:EOS),
]
actual = CLTK::Lexers::EBNF.lex("aaa BBB * + ?")
actual.should eq expected
Expand All @@ -103,7 +102,7 @@ describe "CLTK::Lexer" do
CLTK::Token.new(:A, 0),
CLTK::Token.new(:A, 1),
CLTK::Token.new(:A, 2),
CLTK::Token.new(:EOS)
CLTK::Token.new(:EOS),
]

actual = ENVLexer.lex("aaa")
Expand All @@ -117,26 +116,24 @@ describe "CLTK::Lexer" do
CLTK::Token.new(:A, 3),
CLTK::Token.new(:A, 4),
CLTK::Token.new(:A, 5),
CLTK::Token.new(:EOS)
CLTK::Token.new(:EOS),
]

expected.should eq lexer.lex("aaa")

end

it "test_first_match" do
expected = [
CLTK::Token.new(:APLUS),
CLTK::Token.new(:BPLUS),
CLTK::Token.new(:EOS)
CLTK::Token.new(:EOS),
]

actual = ABFirst.lex("aaabbb")
expected.should eq actual
end

it "test_flags" do

expect_raises(CLTK::Lexer::Exceptions::LexingError) do
FlagLexer.lex("b")
end
Expand All @@ -149,7 +146,7 @@ describe "CLTK::Lexer" do
CLTK::Token.new(:A),
CLTK::Token.new(:B),
CLTK::Token.new(:C),
CLTK::Token.new(:EOS)
CLTK::Token.new(:EOS),
]

actual = FlagLexer.lex("abc")
Expand All @@ -162,7 +159,7 @@ describe "CLTK::Lexer" do
CLTK::Token.new(:A),
CLTK::Token.new(:B),
CLTK::Token.new(:C),
CLTK::Token.new(:EOS)
CLTK::Token.new(:EOS),
]

actual = FlagLexer.lex("abcabc")
Expand All @@ -177,7 +174,7 @@ describe "CLTK::Lexer" do
it "test_longest_match" do
expected = [
CLTK::Token.new(:APLUSBPLUS),
CLTK::Token.new(:EOS)
CLTK::Token.new(:EOS),
]

actual = ABLongest.lex("aaabbb")
Expand All @@ -187,7 +184,7 @@ describe "CLTK::Lexer" do

it "test_match_data" do
expected = [CLTK::Token.new(:FOO, ", ccc"), CLTK::Token.new(:EOS)]
actual = MatchDataLexer.lex("accc")
actual = MatchDataLexer.lex("accc")

actual.should eq expected
end
Expand All @@ -196,7 +193,7 @@ describe "CLTK::Lexer" do
expected = [
CLTK::Token.new(:A),
CLTK::Token.new(:A),
CLTK::Token.new(:EOS)
CLTK::Token.new(:EOS),
]

actual = StateLexer.lex("a (* bbb *) a")
Expand Down
2 changes: 1 addition & 1 deletion spec/cltk/parser_msgpack_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class ArrayCalc2 < CLTK::Parser
clause("PLS e e") { |args| args = args.as(Array); args[1].as(Int32) + args[2].as(Int32) }
clause("SUB e e") { |args| args = args.as(Array); args[1].as(Int32) - args[2].as(Int32) }
clause("MUL e e") { |args| args = args.as(Array); args[1].as(Int32) * args[2].as(Int32) }
clause("DIV e e") { |args| args = args.as(Array); args[1].as(Int32) / args[2].as(Int32) }
clause("DIV e e") { |args| args = args.as(Array); args[1].as(Int32) // args[2].as(Int32) }
nil
end

Expand Down
8 changes: 4 additions & 4 deletions spec/cltk/parser_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class AmbiguousParser < CLTK::Parser
clause("e PLS e") { |e0, op, e1 | e0.as(Int32) + e1.as(Int32) }
clause("e SUB e") { |e0, op, e1 | e0.as(Int32) - e1.as(Int32) }
clause("e MUL e") { |e0, op, e1 | e0.as(Int32) * e1.as(Int32) }
clause("e DIV e") { |e0, op, e1 | e0.as(Int32) / e1.as(Int32) }
clause("e DIV e") { |e0, op, e1 | e0.as(Int32) // e1.as(Int32) }
end

finalize
Expand All @@ -92,7 +92,7 @@ class ArrayCalc < CLTK::Parser
clause("PLS e e") { |args| args = args.as(Array); args[1].as(Int32) + args[2].as(Int32) }
clause("SUB e e") { |args| args = args.as(Array); args[1].as(Int32) - args[2].as(Int32) }
clause("MUL e e") { |args| args = args.as(Array); args[1].as(Int32) * args[2].as(Int32) }
clause("DIV e e") { |args| args = args.as(Array); args[1].as(Int32) / args[2].as(Int32) }
clause("DIV e e") { |args| args = args.as(Array); args[1].as(Int32) // args[2].as(Int32) }
nil
end

Expand Down Expand Up @@ -218,7 +218,7 @@ class ErrorCalc < CLTK::Parser
clause("e PLS e") { |e0, op, e1| e0.as(Int32) + e1.as(Int32) }
clause("e SUB e") { |e0, op, e1| e0.as(Int32) - e1.as(Int32) }
clause("e MUL e") { |e0, op, e1| e0.as(Int32) * e1.as(Int32) }
clause("e DIV e") { |e0, op, e1| e0.as(Int32) / e1.as(Int32) }
clause("e DIV e") { |e0, op, e1| e0.as(Int32) // e1.as(Int32) }
clause("e PLS ERROR e") do |e0, op, ts, e1|
error(ts);
e0.as(Int32) + e1.as(Int32)
Expand Down Expand Up @@ -275,7 +275,7 @@ class RotatingCalc < CLTK::Parser
->(a : Int32, b : Int32) { a + b }, # +
->(a : Int32, b : Int32) { a - b }, # -
->(a : Int32, b : Int32) { a * b }, # *
->(a : Int32, b : Int32) { a / b } # /
->(a : Int32, b : Int32) { a // b } # /
]

def get_op(orig_op)
Expand Down
2 changes: 1 addition & 1 deletion src/cltk/cfg.cr
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ module CLTK
production
end

def callback(&callback: Symbol, Symbol, Production, Array(Int32)-> Nil)
def callback(&callback : Symbol, Symbol, Production, Array(Int32)-> Nil)
@callback = callback
nil
end
Expand Down
4 changes: 3 additions & 1 deletion src/cltk/lexer.cr
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ module CLTK
{% end %}

macro inherited
class Environment < CLTK::Lexer::Environment; end
class Environment < CLTK::Lexer::Environment
property match : Regex::MatchData | StringScanner::StringMatchData | Nil
end
alias ProcType = Proc(String, {{@type}}::Environment, BlockReturn?)
alias FlagsType = Array(Symbol)
alias RuleType = Tuple(FlagsType, ProcType)
Expand Down
4 changes: 2 additions & 2 deletions src/cltk/lexer/environment.cr
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ module CLTK
# @param [Symbol] start_state Lexer's start state.
# @param [Match] match Match object for matching text.

@state: Array(Symbol)
@match: Regex::MatchData?
@state : Array(Symbol)
@match : Regex::MatchData | StringScanner::StringMatchData | Nil

def match
@match.as(Regex::MatchData)
Expand Down
8 changes: 4 additions & 4 deletions src/cltk/parser.cr
Original file line number Diff line number Diff line change
Expand Up @@ -244,11 +244,11 @@ module CLTK
end

# Shorthands for defining productions and clauses
def self.c(expression, precedence = nil, arg_type = @@default_arg_type, &action: Array(Type), Environment -> _)
def self.c(expression, precedence = nil, arg_type = @@default_arg_type, &action : Array(Type), Environment -> _)
self.clause(expression, precedence, arg_type, &action)
end

def self.p(symbol, expression = nil, precedence = nil, arg_type = @@default_arg_type, &action: Array(Type), Environment -> _)
def self.p(symbol, expression = nil, precedence = nil, arg_type = @@default_arg_type, &action : Array(Type), Environment -> _)
self.production(symbol, expression, precedence, arg_type, &action)
end

Expand Down Expand Up @@ -495,7 +495,7 @@ module CLTK
build_up_productions
unless ENV["NOCACHE"]?
if (path = opts[:use]).is_a?(String)
if File.exists?(path) && File.readable?(path)
if File.exists?(path) && File::Info.readable?(path)
finalize_from_serialized_parser(path)
return
end
Expand Down Expand Up @@ -824,7 +824,7 @@ module CLTK
# @param [Proc] proc Code to execute when the block is seen
#
# @return [void]
def self.token_hook(sym, &proc: Proc(Environment, Nil))
def self.token_hook(sym, &proc : Proc(Environment, Nil))
if CFG.is_terminal?(sym)
@@token_hooks[sym.to_s] << proc
else
Expand Down
2 changes: 1 addition & 1 deletion src/cltk/parser/state.cr
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module CLTK
getter :actions

@id = -1
@actions: Hash(String, Array(CLTK::Parser::Action))
@actions : Hash(String, Array(CLTK::Parser::Action))

def initialize(@id : Int32, @actions : Hash(String, Array(CLTK::Parser::Action)), @items : Array(CFG::Item)); end
# Instantiate a new State object.
Expand Down
2 changes: 1 addition & 1 deletion src/cltk/parsers/infix_calc.cr
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ module CLTK
clause("e PLS e") { |e0, op, e1| e0.as(Int32) + e1.as(Int32) }
clause("e SUB e") { |e0, op, e1| e0.as(Int32) - e1.as(Int32) }
clause("e MUL e") { |e0, op, e1| e0.as(Int32) * e1.as(Int32) }
clause("e DIV e") { |e0, op, e1| e0.as(Int32) / e1.as(Int32) }
clause("e DIV e") { |e0, op, e1| e0.as(Int32) // e1.as(Int32) }
end

finalize
Expand Down
2 changes: 1 addition & 1 deletion src/cltk/parsers/postfix_calc.cr
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ module CLTK
clause("e e PLS") { |e0, e1, op| e0.as(Int32) + e1.as(Int32) }
clause("e e SUB") { |e0, e1, op| e0.as(Int32) - e1.as(Int32) }
clause("e e MUL") { |e0, e1, op| e0.as(Int32) * e1.as(Int32) }
clause("e e DIV") { |e0, e1, op| e0.as(Int32) / e1.as(Int32) }
clause("e e DIV") { |e0, e1, op| e0.as(Int32) // e1.as(Int32) }
nil
end

Expand Down
Loading