Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

. improves layout to match code guidelines

  • Loading branch information...
commit 737bdaa376173b0374ddd98e38d40e3b0de0b61d 1 parent 456584d
@kschiess authored
Showing with 16 additions and 7 deletions.
  1. +16 −7 example/boolean_algebra.rb
View
23 example/boolean_algebra.rb
@@ -3,14 +3,16 @@
require "parslet"
require "pp"
-# Parses strings like "var1 and (var2 or var3)" respecting operator precedence and parentheses.
-# After that transforms the parse tree into an array of arrays like this:
+# Parses strings like "var1 and (var2 or var3)" respecting operator precedence
+# and parentheses. After that transforms the parse tree into an array of
+# arrays like this:
#
# [["1", "2"], ["1", "3"]]
#
-# The array represents a DNF (disjunctive normal form).
-# Elements of outer array are connected with "or" operator,
-# while elements of inner arrays are joined with "and".
+# The array represents a DNF (disjunctive normal form). Elements of outer
+# array are connected with "or" operator, while elements of inner arrays are
+# joined with "and".
+#
class Parser < Parslet::Parser
rule(:space) { match[" "].repeat(1) }
rule(:space?) { space.maybe }
@@ -27,8 +29,15 @@ class Parser < Parslet::Parser
rule(:primary) { lparen >> or_operation >> rparen | var }
# Note that following rules are both right-recursive.
- rule(:and_operation) { (primary.as(:left) >> and_operator >> and_operation.as(:right)).as(:and) | primary }
- rule(:or_operation) { (and_operation.as(:left) >> or_operator >> or_operation.as(:right)).as(:or) | and_operation }
+ rule(:and_operation) {
+ (primary.as(:left) >> and_operator >>
+ and_operation.as(:right)).as(:and) |
+ primary }
+
+ rule(:or_operation) {
+ (and_operation.as(:left) >> or_operator >>
+ or_operation.as(:right)).as(:or) |
+ and_operation }
# We start at the lowest precedence rule.
root(:or_operation)
Please sign in to comment.
Something went wrong with that request. Please try again.