Skip to content

Instantly share code, notes, and snippets.

@mikebaldry
Created June 1, 2012 12:31
Show Gist options
  • Select an option

  • Save mikebaldry/2851810 to your computer and use it in GitHub Desktop.

Select an option

Save mikebaldry/2851810 to your computer and use it in GitHub Desktop.
describe "Conditional matching" do
it "should understand binary/unary operators correctly, given the predecate to decide" do
subject = Lexr.that {
ignores /\s+/ => :whitespace
legal_place_for_binary_operator = lambda { |prev| [:addition,
:subtraction,
:multiplication,
:division,
:left_parenthesis,
:start].include? prev.type }
matches "+" => :addition, :unless => legal_place_for_binary_operator
matches "-" => :subtraction, :unless => legal_place_for_binary_operator
matches "*" => :multiplication, :unless => legal_place_for_binary_operator
matches "/" => :division, :unless => legal_place_for_binary_operator
matches "(" => :left_parenthesis
matches ")" => :right_parenthesis
matches /[-+]?[0-9]*\.?[0-9]+/ => :number, :convert_with => lambda { |v| Float(v) }
}
lexer = subject.new("-10*(-3--2)")
lexer.next.should == Lexr::Token.number(-10)
lexer.next.should == Lexr::Token.multiplication("*")
lexer.next.should == Lexr::Token.left_parenthesis("(")
lexer.next.should == Lexr::Token.number(-3)
lexer.next.should == Lexr::Token.subtraction("-")
lexer.next.should == Lexr::Token.number(-2)
lexer.next.should == Lexr::Token.right_parenthesis(")")
lexer.next.should == Lexr::Token.end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment