Skip to content

Commit

Permalink
Renamed Rule#&& and Rule#|| to Rule#& and Rule#|. Also allow Strings …
Browse files Browse the repository at this point in the history
…to be used as parsing rules.
  • Loading branch information
bakkdoor committed Oct 8, 2011
1 parent c1ae01a commit 575d241
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
23 changes: 16 additions & 7 deletions lib/parsing.fy
Expand Up @@ -6,11 +6,11 @@ class Parsing {
}
}

def || other_rule {
def | other_rule {
OrRule new: to_rule and: (other to_rule)
}

def && other {
def & other {
AndRule new: to_rule and: (other to_rule)
}

Expand All @@ -32,14 +32,22 @@ class Parsing {
}
}

class Regexp {
class RegexpParsing {
include: Rules

def to_rule {
Rule new: self
}
}

class StringParsing {
include: Rules

def to_rule {
Regexp new(self) to_rule
}
}

class Rule {
include: Rules
read_write_slots: ['name, 'pattern, 'action]
Expand Down Expand Up @@ -148,16 +156,16 @@ class Parsing {
rule = @rule
if: @max then: {
@min upto: @max do: {
rule = rule && @rule
rule = rule & @rule
}
rule = rule && (@rule not)
rule = rule & (@rule not)
rule action: @action
val = rule parse: string offset: offset
@offset = rule offset + offset
return val
} else: {
@min times: {
rule = rule && @rule
rule = rule & @rule
}
@offset = offset
vals = []
Expand All @@ -176,4 +184,5 @@ class Parsing {
}
}

Regexp include: Parsing Regexp
Regexp include: Parsing RegexpParsing
String include: Parsing StringParsing
7 changes: 3 additions & 4 deletions tests/parsing.fy
Expand Up @@ -22,18 +22,17 @@ FancySpec describe: Parsing with: {
}

it: "creates a simple ast" when: {
c = /class/
space = /\s+/
identifier = space && /(\S+)/ #&& (space optional)
class_def = c && identifier ==> |_ classname| {
identifier = space & /(\S+)/ #& (space optional)
class_def = "class" & identifier ==> |_ classname| {
('class_def, classname[1] to_sym)
}

class_def parse: "class Foo" . is: ('class_def, 'Foo)
}

it: "parses optional rules" when: {
opt = /a/ optional ==> {
opt = "a" optional ==> {
'yes
}

Expand Down

0 comments on commit 575d241

Please sign in to comment.