Skip to content

Commit

Permalink
Parses correctly parameter lists and parenthesis and named arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
Amos Wenger committed Sep 15, 2011
1 parent efef79f commit ad33ac4
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 11 deletions.
10 changes: 5 additions & 5 deletions source/herge/GrammarReader.ooc
Expand Up @@ -10,11 +10,11 @@ GrammarReader: class extends StringReader {
super(string)
}

readRegexp: func (r: Regexp) -> String {
readRegexp: func (r: Regexp, groupIndex := 0) -> String {
_match := r matches(string, marker, string size)
if(_match) {
sub := _match group(0)
marker += sub length()
if(_match && _match groupStart(0) == marker) {
sub := _match group(groupIndex)
marker += _match groupLength(0)
sub
} else {
""
Expand All @@ -39,7 +39,7 @@ GrammarReader: class extends StringReader {

line := readLine()

"Parsing error at %d: %s\n> %s\n %s" printfln(marker, message, line, " " times(errorMarker - startOfLine) + "~")
"Parsing error at %d: %s\n> %s\n %s" printfln(errorMarker, message, line, " " times(errorMarker - startOfLine) + "~")
exit(1)
}

Expand Down
42 changes: 36 additions & 6 deletions source/herge/main.ooc
Expand Up @@ -79,6 +79,19 @@ InstanceRule: class extends Rule {

}

NamedRule: class extends Rule {

name: String
instance: Rule

init: func(=name, =instance)

toString: func -> String {
"%s:%s" format(name, instance _)
}

}

RegexpRule: class extends Rule {

expr: String
Expand Down Expand Up @@ -160,7 +173,8 @@ AndRule: class extends Rule {
Grammar: class {

// ----- Regular expressions
word := Regexp compile("[A-Za-z-]*")
word := Regexp compile("[A-Za-z\\-]*")
wordColon := Regexp compile("([A-Za-z\\-]*):")
allWs := Regexp compile("[ \t\n]*")
ws := Regexp compile("[ \t]*")
assDecl := Regexp compile(":=[ \t]*")
Expand Down Expand Up @@ -207,6 +221,9 @@ Grammar: class {
}

readRule: func -> Rule {
ruleName := reader readRegexp(wordColon, 1)
skipWhitespace()

rule := match (reader peek()) {
case '"' =>
reader read()
Expand All @@ -216,15 +233,18 @@ Grammar: class {
expr := "[%s]" format(reader readUntil(']'))
RegexpRule new(expr)
case '(' =>
"Reading paren" println()
reader read()
rule := readRule()
skipWhitespace()
if(reader read() != ')') reader error("Expected closing parenthesis")
rule
case =>
name := reader readRegexp(word)
if(name empty?()) reader error("Expected rule here")
instance := InstanceRule new(name)
instanceName := reader readRegexp(word)
"Got instanceName %s" printfln(instanceName)
if(instanceName empty?()) reader error("Expected rule here")

instance := InstanceRule new(instanceName)
skipWhitespace()
if(reader peek() == '<') {
reader read()
Expand All @@ -236,11 +256,16 @@ Grammar: class {
reader error("Invalid instance param list, expected comma")
}
}
reader read()
if(reader read() != '>') reader error("Expected '>' here")
}

instance
}

if(ruleName) rule = NamedRule new(ruleName trim(':'), rule)

"Just read %s %s" printfln(rule class name, rule _)

while (true) {
skipWhitespace()
match (reader peek()) {
Expand All @@ -259,13 +284,18 @@ Grammar: class {
skipAllWhitespace()
case '\n' =>
break
case '>' =>
break
case ')' =>
break

case =>
rightRule := readRule()
rule = AndRule new(rule, rightRule)
}
"Just read %s %s" printfln(rule class name, rule _)
}

"Just read %s %s" printfln(rule class name, rule _)
rule
}

Expand Down

0 comments on commit ad33ac4

Please sign in to comment.