@ -1,5 +1,8 @@
// ----- Utils -----
true = (1 == 1);
false = (1 == 0);
Object.contains(item) {
for (i in self.length()) {
if (self[i] == item) {
@ -109,30 +112,22 @@ Object.subtype(name) { self.new(__name__: name) }
// Input stream
Stream = Object.subtype(#Stream);
// Stream = Object.subtype(#Stream);
newStream(string) {
let self = Stream.new(
content: string,
position: 0,
limit: len(string)
);
self;
}
// newStream(string) {
// let self = Stream.new(
// content: string,
// position: 0,
// limit: len(string)
// );
// self;
// }
Stream.atEnd() { self.position >= self.limit }
// Stream.atEnd() { self.position >= self.limit }
Stream.peek() { !self.atEnd() && self.content[self.position] }
Stream.inc() { !self.atEnd() && { self.position = self.position + 1; } }
Stream.next() {
!self.atEnd() && {
let c = self.content[self.position];
self.position = self.position + 1;
c;
}
}
//Stream.inc() { !self.atEnd() && { self.position = self.position + 1; } }
Stream.setLastBegin = () { self.lastBegin = self.position; };
//Stream.setLastBegin = () { self.lastBegin = self.position; };
// Context
@ -156,27 +151,36 @@ Context.declareVariable(var) {
StringLiteral = Object.subtype(#StringLiteral);
StringLiteral.match(stream, context, actions) {
let n = len(self.string);
let i = 0;
let success = 1;
if (stream.atEnd()) { success = 0; }
let startPosition = stream.position;
while(i < n && success == 1) {
if (self.string[i] != stream.peek()) {
success = 0;
stream.position = startPosition;
} else {
i = i + 1;
stream.inc();
}
StringLiteral.match(stream, context, actions) {
if (stream.match(self.string)) {
stream.position += len(self.string);
true;
} else {
false;
}
success;
}
//StringLiteral.match(stream, context, actions) {
//
// let n = len(self.string);
// let i = 0;
// let success = 1;
// if (stream.atEnd()) { success = 0; }
// let startPosition = stream.position;
//
// while(i < n && success == 1) {
// if (self.string[i] != stream.peek()) {
// success = 0;
// stream.position = startPosition;
//
// } else {
// i = i + 1;
// stream.inc();
// }
// }
// success;
//}
// Character Class
CharacterClass = Object.subtype(#CharacterClass);
@ -240,7 +244,7 @@ CharacterClass.match(stream, context, actions) {
stream.inc();
}
success;
success == 1 ;
}
// Dot
@ -250,9 +254,9 @@ Dot = Object.subtype(#Dot);
Dot.match(stream, context, actions) {
if (!stream.atEnd()) {
stream.inc();
1 ;
true ;
} else {
0 ;
false ;
}
}
@ -262,7 +266,7 @@ Begin = Object.subtype(#Begin);
Begin.match(stream, context, actions) {
stream.setLastBegin();
1 ;
true ;
}
// End
@ -271,7 +275,7 @@ End = Object.subtype(#End);
End.match(stream, context, actions) {
context.variables.yytext = stream.content[stream.lastBegin..stream.position].unescaped();
1 ;
true ;
}
// Optional (? postfix operator)
@ -279,18 +283,8 @@ End.match(stream, context, actions) {
Optional = Object.subtype(#Optional);
Optional.match(stream, context, actions) {
let initialActionCount = actions.length();
let startingPosition = stream.position;
let success = self.expression.match(stream, context, actions);
if (success == 0) {
while (actions.length() > initialActionCount) {
actions.pop();
}
stream.position = startingPosition;
}
1;
self.expression.match(stream, context, actions);
true;
}
// Star
@ -298,8 +292,8 @@ Optional.match(stream, context, actions) {
Star = Object.subtype(#Star);
Star.match(stream, context, actions) {
while (self.expression.match(stream, context, actions) == 1 ) {}
1 ;
while (self.expression.match(stream, context, actions)) {}
true ;
}
// Plus
@ -307,11 +301,11 @@ Star.match(stream, context, actions) {
Plus = Object.subtype(#Plus);
Plus.match(stream, context, actions) {
if (self.expression.match(stream, context, actions) == 1 ) {
while (self.expression.match(stream, context, actions) == 1 ) {}
1 ;
if (self.expression.match(stream, context, actions) == true ) {
while (self.expression.match(stream, context, actions) == true ) {}
true ;
} else {
0 ;
false ;
}
}
@ -322,11 +316,11 @@ And = Object.subtype(#And);
And.match(stream, context, actions) {
let position = stream.position;
if (self.expression.match(stream, context, actions) == 1 ) {
if (self.expression.match(stream, context, actions) == true ) {
stream.position = position;
1 ;
true ;
} else {
0 ;
false ;
}
}
@ -337,11 +331,11 @@ Not = Object.subtype(#Not);
Not.match(stream, context, actions) {
let position = stream.position;
if (self.expression.match(stream, context, actions) == 1 ) {
if (self.expression.match(stream, context, actions) == true ) {
stream.position = position;
0 ;
false ;
} else {
1 ;
true ;
}
}
@ -349,38 +343,42 @@ Not.match(stream, context, actions) {
Sequence = Object.subtype(#Sequence);
Sequence._match(stream, context, actions, index) {
if (index == self.length() - 1) {
return self[index].match(stream, context, actions);
}
self[index].match(stream, context, actions) && self._match(stream, context, actions, index + 1);
}
Sequence.match(stream, context, actions) {
let i = 0;
let match = 1;
while (i < self.length() && match == 1) {
match = self[i].match(stream, context, actions);
i = i + 1;
let initialActionCount = actions.length();
let startingPosition = stream.position;
success = self._match(stream, context, actions, 0);
if (success == false) {
while (actions.length() > initialActionCount) {
actions.pop();
}
stream.position = startingPosition;
}
match;
success;
}
// Alternation
Alternation = Object.subtype(#Alternation);
Alternation.match(stream, context, actions) {
let i = 0;
let success = 0;
while (i < self.length() && success == 0) {
let initialActionCount = actions.length();
let startingPosition = stream.position;
success = self[i].match(stream, context, actions);
if (success == 0) {
while (actions.length() > initialActionCount) {
actions.pop();
}
stream.position = startingPosition;
}
i = i + 1;
Alternation._match(stream, context, actions, index) {
if (index == self.length() - 1) {
return self[index].match(stream, context, actions);
}
success;
self[index].match(stream, context, actions) || self._match(stream, context, actions, index + 1);
}
Alternation.match(stream, context, actions) {
return self._match(stream, context, actions, 0);
}
// Action
@ -389,7 +387,7 @@ Action = Object.subtype(#Action);
Action.match(stream, context, actions) {
actions.push([action: self, context: context]);
1 ;
true ;
}
Action.execute(context) {
@ -416,9 +414,9 @@ ParseTimeAction = Object.subtype(#ParseTimeAction);
ParseTimeAction.match(stream, context, actions) {
if(self.action.execute(context)) {
1 ;
true ;
} else {
0 ;
false ;
}
}
@ -695,7 +693,7 @@ grammar = Sequence.new()
.push(RuleCall.new(name: #ws))
.push(Plus.new(expression: Sequence.new()
.push(Assignment.new(name: #d, rule: RuleCall.new(name: #definition)))
.push(Action.new(parseTree: `{ set(d.name, d.expression); print("Parsed rule ", d.name, "\n"); }))
.push(Action.new(parseTree: `{ set(d.name, d.expression); }))
))
.push(RuleCall.new(name: #end_of_file));
@ -963,7 +961,6 @@ ws = Star.new(expression: Alternation.new()
with(#reading);
stream = newStream(readfile("rawminproto.leg"));
context = Context.new(outerContext: nil).init();
actions = [];