Skip to content
Browse files

Reworking the parser to parse e.b.a().c left recursive and the rest

right-recursive.
  • Loading branch information...
1 parent 35a3ded commit f37ca4e51c3dda116856c592b6b39e791c948544 @zefhemel committed Mar 27, 2009
Showing with 71 additions and 30 deletions.
  1. +16 −2 ast.pil
  2. +13 −7 gelly.pil
  3. +41 −20 parser.pil
  4. +1 −1 webdsl.g
View
18 ast.pil
@@ -58,7 +58,7 @@ class gel::OpTerm extends gel::Term {
s.append(op);
var sawCons = false;
if(!isPrefix()) {
- if(left instanceof OpTerm) {
+ if(left instanceof OpTerm || left instanceof GroupTerm) {
s.append("\n");
sawCons = true;
} else {
@@ -67,7 +67,7 @@ class gel::OpTerm extends gel::Term {
s.append(left.toIndentedString(depth+1));
}
if(!isPostfix()) {
- if(right instanceof OpTerm) {
+ if(right instanceof OpTerm || right instanceof GroupTerm) {
s.append("\n");
} else if(sawCons) {
s.append("\n");
@@ -196,6 +196,20 @@ class gel::GroupTerm extends gel::Term {
return new GroupTerm(type, group);
}
+ String toIndentedString(Int depth) {
+ var s = new MutableString();
+ s.append(spaces(depth));
+ s.append("(");
+ s.append(type);
+ if(group instanceof OpTerm || group instanceof GroupTerm) {
+ s.append("\n");
+ } else {
+ s.append(' ');
+ }
+ s.append(group.toIndentedString(depth+1));
+ s.append(')');
+ return s.as<String>;
+ }
as<String> {
var ms = new MutableString();
ms.append('(');
View
20 gelly.pil
@@ -14,31 +14,37 @@ void main(Array<String> args) {
initPrimitives();
String input = null;
//println(replaceMatchVar(new Parser("![ `exp ]").acceptExp()).toIndentedString(0));
- //println(new Parser("<bla> ha").acceptExp().toIndentedString(0));
- var env = new Env(new Map<String, StrategyDef>(), new Map<String, ClosureStrategyDef>(), new Map<String, Term>());
- if(!env.evalString("import \"sugar.g\"")) {
- println("Couldn't load sugar.g");
- }
- if(args.length > 0) {
+ println(new Parser("a++ + 7--").acceptExp().toIndentedString(0));
+ println(new Parser("a.b.c()").acceptExp().toIndentedString(0));
+ println(new Parser("a().b").acceptExp().toIndentedString(0));
+ println(new Parser("a().b()").acceptExp().toIndentedString(0));
+ //if(true) {
+ //return;
+ //}
+ /*if(args.length > 0) {
if(env.evalString("import \"" + args[0] + "\"")) {
println(pretty(env.currentTerm, 0));
} else {
println("Rewriting failed.");
}
} else {
+ */
println("Welcome to gelly, type :q to quit.");
while(input != ":q") {
print("> ");
input = readln();
+ println(new Parser(input).acceptExp().toIndentedString(0));
+ /*
if(input != ":q") {
if(env.evalString(input)) {
println(env.currentTerm.toIndentedString(0));
} else {
println("Rewriting failed.");
}
}
+ */
}
- }
+ //}
/*
println(new Parser("if: 3 == 0").acceptExp().toIndentedString(0));
println(new Parser("if 3 + if 8").acceptExp().toIndentedString(0));
View
61 parser.pil
@@ -24,11 +24,11 @@ class gel::Parser {
Int followLevel = 0;
Array<Array<String>> operators = new Array<Array<String>>(
- new Array<String>("."),
+ new Array<String>(".", "_"),
new Array<String>("*", "/", "%"),
new Array<String>("+", "-"),
new Array<String>("<", "<=", ">", ">="),
- new Array<String>("!", "=", "+=", "-=", "*=", ":="),
+ new Array<String>("!", "=", "+=", "-=", "*=", ":=", "!="),
new Array<String>("&"),
new Array<String>("|"),
new Array<String>("?", ":", "$"),
@@ -37,7 +37,7 @@ class gel::Parser {
new Array<String>(";")
);
- List<String> noWhitespaceOps = new List<String>(";", ",", ".");
+ List<String> noWhitespaceOps = new List<String>(";", ",", ".", "_");
List<Char> allOps = new List<Char>('!', '@', '#', '%', '^', '&', '*', '-', '+', '=', '|', '/', '\\', '<', '>', '.', '?');
Array<String> prefixOps = new Array<String>("+", "-", "*", "<", ">", "?", "!", ":", "`", "$");
@@ -191,27 +191,41 @@ class gel::Parser {
var didAcceptWhitespace = acceptWhiteSpace();
var op = acceptOps(operators[level]);
if(!didAcceptWhitespace && op != null && !noWhitespaceOps.contains(op)) {
- i = oldI;
- if(level == followLevel) {
- return followFactor(t);
- } else {
- return t;
- }
+ //if(level == 0) { // Postfix
+ t = new OpTerm(op, t, null);
+ acceptWhiteSpace();
+ oldI = i;
+ op = acceptOps(operators[level]);
+ //} else {
+ //i = oldI;
+ //return t;
+ //}
+ //if(level == followLevel) {
+ //return followFactor(t);
+ //} else {
+ //}
}
while(op != null) {
if(!acceptWhiteSpace() && !noWhitespaceOps.contains(op)) {
+ /*
i = oldI;
return t;
+ */
+ t = new OpTerm(op, t, null);
+ acceptWhiteSpace();
+ op = acceptOps(operators[level]);
}
Term t2 = null;
- /*
if(level == 0) {
- t2 = acceptFactor();
+ t2 = acceptFactor(); // One level lower to ennforce left-recursive parsing
} else {
- t2 = acceptGeneric(level-1);
+ t2 = acceptGeneric(level);
}
- */
- t2 = acceptGeneric(level);
+ if(t2 == null && op == "_") {
+ i = oldI;
+ return t;
+ }
+ //t2 = acceptGeneric(level);
t = new OpTerm(op, t, valueOrEmpty(t2));
oldI = i;
if(!acceptWhiteSpace() && !noWhitespaceOps.contains(op)) {
@@ -221,11 +235,11 @@ class gel::Parser {
op = acceptOps(operators[level]);
}
i = oldI;
- if(level == followLevel) {
- return followFactor(t);
- } else {
+ //if(level == followLevel) {
+ //return followFactor(t);
+ //} else {
return t;
- }
+ //}
}
Term acceptExp() {
@@ -278,8 +292,11 @@ class gel::Parser {
}
String acceptOps(Array<String> ops) {
+ var canBeWhiteSpace = false;
for(String op : ops) {
- if(lookAhead(op)) {
+ if(op == "_") {
+ canBeWhiteSpace = true;
+ } else if(lookAhead(op)) {
var opstr = new MutableString(op);
var oldI = i;
i = i + op.length;
@@ -294,7 +311,11 @@ class gel::Parser {
}
}
}
- return null;
+ if(canBeWhiteSpace) {
+ return "_";
+ } else {
+ return null;
+ }
}
String acceptKeyword() {
View
2 webdsl.g
@@ -9,7 +9,7 @@ main[
strategy single-to-code {
?[ `s(`a) ];
- ![ `s(`a) ingore ];
+ ![ `s(`a) ignore ];
elem-to-code;
};

0 comments on commit f37ca4e

Please sign in to comment.
Something went wrong with that request. Please try again.