|
@ -119,22 +119,7 @@ Object.last() { self[self.length() - 1] } |
|
|
|
|
|
|
|
|
// Input stream |
|
|
// Input stream |
|
|
|
|
|
|
|
|
//Stream = Object.subtype(#Stream); |
|
|
|
|
|
|
|
|
|
|
|
//newStream(string) { |
|
|
|
|
|
// self = Stream.new( |
|
|
|
|
|
// content: string, |
|
|
|
|
|
// position: 0, |
|
|
|
|
|
// limit: len(string) |
|
|
|
|
|
// ); |
|
|
|
|
|
// self; |
|
|
|
|
|
//} |
|
|
|
|
|
|
|
|
|
|
|
//Stream.atEnd() { self.position >= self.limit } |
|
|
|
|
|
Stream.peek() { self.content[self.position] } |
|
|
Stream.peek() { self.content[self.position] } |
|
|
//Stream.inc() { !self.atEnd() && { self.position = self.position + 1; } } |
|
|
|
|
|
|
|
|
|
|
|
//Stream.setLastBegin = () { self.lastBegin = self.position; }; |
|
|
|
|
|
|
|
|
|
|
|
// Context |
|
|
// Context |
|
|
|
|
|
|
|
@ -158,196 +143,29 @@ Context.declareVariable(var) { |
|
|
|
|
|
|
|
|
StringLiteral = Object.subtype(#StringLiteral); |
|
|
StringLiteral = Object.subtype(#StringLiteral); |
|
|
|
|
|
|
|
|
StringLiteral.match(stream, context, actions) { |
|
|
|
|
|
if (stream.match(self.string)) { |
|
|
|
|
|
stream.position += len(self.string); |
|
|
|
|
|
true; |
|
|
|
|
|
} else { |
|
|
|
|
|
false; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
StringLiteral.getMatchingExpression() { |
|
|
StringLiteral.getMatchingExpression() { |
|
|
//return `(@stream).match(@self.string) && ((@stream).position += len(@self.string)) && @true; |
|
|
|
|
|
return `{ |
|
|
return `{ |
|
|
if (stream.content.compareFrom(stream.position, @self.string) == 0) { |
|
|
|
|
|
stream.position += len(@self.string); |
|
|
|
|
|
@true; |
|
|
|
|
|
} else { |
|
|
|
|
|
@false; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
((stream.content.compareFrom(stream.position, @self.string) == 0) && |
|
|
|
|
|
(stream.position += len(@self.string)) && |
|
|
|
|
|
@true) |
|
|
|
|
|
|| |
|
|
|
|
|
@false |
|
|
}; |
|
|
}; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
//StringLiteral.match(stream, context, actions) { |
|
|
|
|
|
// |
|
|
|
|
|
// n = len(self.string); |
|
|
|
|
|
// i = 0; |
|
|
|
|
|
// success = 1; |
|
|
|
|
|
// if (stream.atEnd()) { success = 0; } |
|
|
|
|
|
// 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 |
|
|
// Character Class |
|
|
|
|
|
|
|
|
CharacterClass = Object.subtype(#CharacterClass); |
|
|
CharacterClass = Object.subtype(#CharacterClass); |
|
|
|
|
|
|
|
|
CharacterClass.match(stream, context, actions) { |
|
|
|
|
|
|
|
|
|
|
|
local classLength = len(self.value); |
|
|
|
|
|
local i = 0; |
|
|
|
|
|
local prevChar = nil; |
|
|
|
|
|
local success = 0; |
|
|
|
|
|
|
|
|
|
|
|
while (i < classLength && success == 0 && !stream.atEnd()) { |
|
|
|
|
|
|
|
|
|
|
|
// [a] case |
|
|
|
|
|
if (prevChar == nil) { |
|
|
|
|
|
// println("[a] case"); |
|
|
|
|
|
prevChar = self.value[i]; |
|
|
|
|
|
|
|
|
|
|
|
if (stream.peek() == self.value[i]) { |
|
|
|
|
|
success = 1; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} else if (prevChar != nil && self.value[i] == '-') { |
|
|
|
|
|
|
|
|
|
|
|
// [a-z] case |
|
|
|
|
|
if (i+1 < classLength) { |
|
|
|
|
|
// println("[a-z] case"); |
|
|
|
|
|
local rangeStart = prevChar; |
|
|
|
|
|
local rangeEnd = self.value[i+1]; |
|
|
|
|
|
// print("Range Start: ", rangeStart, " | "); |
|
|
|
|
|
// print("Range End: ", rangeEnd, "\n"); |
|
|
|
|
|
if (stream.peek() >= rangeStart |
|
|
|
|
|
&& stream.peek() <= rangeEnd |
|
|
|
|
|
) { |
|
|
|
|
|
success = 1; |
|
|
|
|
|
} |
|
|
|
|
|
prevChar = nil; |
|
|
|
|
|
i = i + 1; |
|
|
|
|
|
|
|
|
|
|
|
// [a-] case |
|
|
|
|
|
} else { |
|
|
|
|
|
// println("[a-] case"); |
|
|
|
|
|
if (stream.peek() == '-') { |
|
|
|
|
|
success = 1; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// [ab] case |
|
|
|
|
|
} else if (prevChar != nil && self.value[i] != '-') { |
|
|
|
|
|
// println("[ab] case"); |
|
|
|
|
|
prevChar = self.value[i]; |
|
|
|
|
|
if (stream.peek() == self.value[i]) { |
|
|
|
|
|
success = 1; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
// print("prevChar: ", prevChar, "\n"); |
|
|
|
|
|
i = i + 1; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (success == 1) { |
|
|
|
|
|
stream.inc(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
success == 1; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
CharacterClass.staticmatch(stream, context, actions, value) { |
|
|
|
|
|
|
|
|
|
|
|
local classLength = len(value); |
|
|
|
|
|
local i = 0; |
|
|
|
|
|
local prevChar = nil; |
|
|
|
|
|
local success = 0; |
|
|
|
|
|
|
|
|
|
|
|
while (i < classLength && success == 0 && !stream.atEnd()) { |
|
|
|
|
|
|
|
|
|
|
|
// [a] case |
|
|
|
|
|
if (prevChar == nil) { |
|
|
|
|
|
//println("[a] case"); |
|
|
|
|
|
prevChar = value[i]; |
|
|
|
|
|
|
|
|
|
|
|
if (stream.peek() == value[i]) { |
|
|
|
|
|
success = 1; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} else if (prevChar != nil && value[i] == '-') { |
|
|
|
|
|
|
|
|
|
|
|
// [a-z] case |
|
|
|
|
|
if (i+1 < classLength) { |
|
|
|
|
|
// println("[a-z] case"); |
|
|
|
|
|
local rangeStart = prevChar; |
|
|
|
|
|
local rangeEnd = value[i+1]; |
|
|
|
|
|
// print("Range Start: ", rangeStart, " | "); |
|
|
|
|
|
// print("Range End: ", rangeEnd, "\n"); |
|
|
|
|
|
if (stream.peek() >= rangeStart |
|
|
|
|
|
&& stream.peek() <= rangeEnd |
|
|
|
|
|
) { |
|
|
|
|
|
success = 1; |
|
|
|
|
|
} |
|
|
|
|
|
prevChar = nil; |
|
|
|
|
|
i = i + 1; |
|
|
|
|
|
|
|
|
|
|
|
// [a-] case |
|
|
|
|
|
} else { |
|
|
|
|
|
// println("[a-] case"); |
|
|
|
|
|
if (stream.peek() == '-') { |
|
|
|
|
|
success = 1; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// [ab] case |
|
|
|
|
|
} else if (prevChar != nil && value[i] != '-') { |
|
|
|
|
|
// println("[ab] case"); |
|
|
|
|
|
prevChar = value[i]; |
|
|
|
|
|
if (stream.peek() == value[i]) { |
|
|
|
|
|
success = 1; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
// print("prevChar: ", prevChar, "\n"); |
|
|
|
|
|
i = i + 1; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (success == 1) { |
|
|
|
|
|
stream.inc(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
success == 1; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
CharacterClass.getMatchingExpression() { |
|
|
CharacterClass.getMatchingExpression() { |
|
|
//return `CharacterClass.staticmatch(@stream, @context, @actions, @self.value); |
|
|
|
|
|
return `{ !stream.atEnd() && (@self.value).charClass().bitTest(stream.peek()) && stream.inc() && @true }; |
|
|
|
|
|
|
|
|
return `{ !stream.atEnd() && (@self.value.charClass()).bitTest(stream.peek()) && stream.inc() && @true }; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Dot |
|
|
// Dot |
|
|
|
|
|
|
|
|
Dot = Object.subtype(#Dot); |
|
|
Dot = Object.subtype(#Dot); |
|
|
|
|
|
|
|
|
Dot.match(stream, context, actions) { |
|
|
|
|
|
if (!stream.atEnd()) { |
|
|
|
|
|
stream.inc(); |
|
|
|
|
|
true; |
|
|
|
|
|
} else { |
|
|
|
|
|
false; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
Dot.getMatchingExpression() { |
|
|
Dot.getMatchingExpression() { |
|
|
//return `!(@stream).atEnd() && (@stream).inc() && @true; |
|
|
|
|
|
return `{ !stream.atEnd() && stream.inc() && @true }; |
|
|
return `{ !stream.atEnd() && stream.inc() && @true }; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -355,13 +173,7 @@ Dot.getMatchingExpression() { |
|
|
|
|
|
|
|
|
Begin = Object.subtype(#Begin); |
|
|
Begin = Object.subtype(#Begin); |
|
|
|
|
|
|
|
|
Begin.match(stream, context, actions) { |
|
|
|
|
|
stream.setLastBegin(); |
|
|
|
|
|
true; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
Begin.getMatchingExpression() { |
|
|
Begin.getMatchingExpression() { |
|
|
//return `(@stream).setLastBegin() && true; |
|
|
|
|
|
return `{ stream.setLastBegin() && @true }; |
|
|
return `{ stream.setLastBegin() && @true }; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -369,14 +181,7 @@ Begin.getMatchingExpression() { |
|
|
|
|
|
|
|
|
End = Object.subtype(#End); |
|
|
End = Object.subtype(#End); |
|
|
|
|
|
|
|
|
End.match(stream, context, actions) { |
|
|
|
|
|
context.variables.yytext = stream.content[stream.lastBegin..stream.position].unescaped(); |
|
|
|
|
|
true; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
End.getMatchingExpression() { |
|
|
End.getMatchingExpression() { |
|
|
//return `((@context.variables).yytext = (@stream.content)[(@stream).lastBegin..(@stream).position].unescaped()) |
|
|
|
|
|
// && true; |
|
|
|
|
|
return `{ (context.variables.yytext = stream.content[stream.lastBegin..stream.position].unescaped()) |
|
|
return `{ (context.variables.yytext = stream.content[stream.lastBegin..stream.position].unescaped()) |
|
|
&& @true }; |
|
|
&& @true }; |
|
|
} |
|
|
} |
|
@ -385,13 +190,7 @@ End.getMatchingExpression() { |
|
|
|
|
|
|
|
|
Optional = Object.subtype(#Optional); |
|
|
Optional = Object.subtype(#Optional); |
|
|
|
|
|
|
|
|
Optional.match(stream, context, actions) { |
|
|
|
|
|
self.expression.match(stream, context, actions); |
|
|
|
|
|
true; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
Optional.getMatchingExpression() { |
|
|
Optional.getMatchingExpression() { |
|
|
//return `(@self.expression.getMatchingExpression(stream, context, actions)) || @true; |
|
|
|
|
|
return `{ (@self.expression.getMatchingExpression()); @true }; |
|
|
return `{ (@self.expression.getMatchingExpression()); @true }; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -399,28 +198,14 @@ Optional.getMatchingExpression() { |
|
|
|
|
|
|
|
|
Star = Object.subtype(#Star); |
|
|
Star = Object.subtype(#Star); |
|
|
|
|
|
|
|
|
Star.match(stream, context, actions) { |
|
|
|
|
|
while (self.expression.match(stream, context, actions)) {} |
|
|
|
|
|
true; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
Star.getMatchingExpression() { |
|
|
Star.getMatchingExpression() { |
|
|
return `{ while (@self.expression.getMatchingExpression()) {} @true }; |
|
|
|
|
|
|
|
|
return `{ while (@self.expression.getMatchingExpression()) @true; @true }; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Plus |
|
|
// Plus |
|
|
|
|
|
|
|
|
Plus = Object.subtype(#Plus); |
|
|
Plus = Object.subtype(#Plus); |
|
|
|
|
|
|
|
|
Plus.match(stream, context, actions) { |
|
|
|
|
|
if (self.expression.match(stream, context, actions) == true) { |
|
|
|
|
|
while (self.expression.match(stream, context, actions) == true) {} |
|
|
|
|
|
true; |
|
|
|
|
|
} else { |
|
|
|
|
|
false; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
Plus.getMatchingExpression() { |
|
|
Plus.getMatchingExpression() { |
|
|
return `{ |
|
|
return `{ |
|
|
if (@self.expression.getMatchingExpression()) { |
|
|
if (@self.expression.getMatchingExpression()) { |
|
@ -436,17 +221,6 @@ Plus.getMatchingExpression() { |
|
|
|
|
|
|
|
|
And = Object.subtype(#And); |
|
|
And = Object.subtype(#And); |
|
|
|
|
|
|
|
|
And.match(stream, context, actions) { |
|
|
|
|
|
local position = stream.position; |
|
|
|
|
|
|
|
|
|
|
|
if (self.expression.match(stream, context, actions) == true) { |
|
|
|
|
|
stream.position = position; |
|
|
|
|
|
true; |
|
|
|
|
|
} else { |
|
|
|
|
|
false; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
And.getMatchingExpression() { |
|
|
And.getMatchingExpression() { |
|
|
return `{ |
|
|
return `{ |
|
|
local position = stream.position; |
|
|
local position = stream.position; |
|
@ -463,17 +237,6 @@ And.getMatchingExpression() { |
|
|
|
|
|
|
|
|
Not = Object.subtype(#Not); |
|
|
Not = Object.subtype(#Not); |
|
|
|
|
|
|
|
|
Not.match(stream, context, actions) { |
|
|
|
|
|
local position = stream.position; |
|
|
|
|
|
|
|
|
|
|
|
if (self.expression.match(stream, context, actions) == true) { |
|
|
|
|
|
stream.position = position; |
|
|
|
|
|
false; |
|
|
|
|
|
} else { |
|
|
|
|
|
true; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
Not.getMatchingExpression() { |
|
|
Not.getMatchingExpression() { |
|
|
return `{ |
|
|
return `{ |
|
|
local position = stream.position; |
|
|
local position = stream.position; |
|
@ -490,30 +253,6 @@ Not.getMatchingExpression() { |
|
|
|
|
|
|
|
|
Sequence = Object.subtype(#Sequence); |
|
|
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) { |
|
|
|
|
|
local initialActionCount = actions.length(); |
|
|
|
|
|
local startingPosition = stream.position; |
|
|
|
|
|
|
|
|
|
|
|
local success = self._match(stream, context, actions, 0); |
|
|
|
|
|
|
|
|
|
|
|
if (success == false) { |
|
|
|
|
|
while (actions.length() > initialActionCount) { |
|
|
|
|
|
actions.pop(); |
|
|
|
|
|
} |
|
|
|
|
|
stream.position = startingPosition; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
success; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Sequence._getMatchingExpression(index) { |
|
|
Sequence._getMatchingExpression(index) { |
|
|
if (index == self.length() - 1) { |
|
|
if (index == self.length() - 1) { |
|
|
return self[index].getMatchingExpression(); |
|
|
return self[index].getMatchingExpression(); |
|
@ -529,9 +268,8 @@ Sequence.getMatchingExpression() { |
|
|
({ |
|
|
({ |
|
|
(@self._getMatchingExpression(0)); |
|
|
(@self._getMatchingExpression(0)); |
|
|
} || { |
|
|
} || { |
|
|
while (actions.length() > initialActionCount) { |
|
|
|
|
|
|
|
|
while (actions.length() > initialActionCount) |
|
|
actions.pop(); |
|
|
actions.pop(); |
|
|
} |
|
|
|
|
|
stream.position = startingPosition; |
|
|
stream.position = startingPosition; |
|
|
@false; |
|
|
@false; |
|
|
}); |
|
|
}); |
|
@ -542,17 +280,6 @@ Sequence.getMatchingExpression() { |
|
|
|
|
|
|
|
|
Alternation = Object.subtype(#Alternation); |
|
|
Alternation = Object.subtype(#Alternation); |
|
|
|
|
|
|
|
|
Alternation._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); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
Alternation.match(stream, context, actions) { |
|
|
|
|
|
return self._match(stream, context, actions, 0); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
Alternation._getMatchingExpression(index) { |
|
|
Alternation._getMatchingExpression(index) { |
|
|
if (index == self.length() - 1) { |
|
|
if (index == self.length() - 1) { |
|
|
return self[index].getMatchingExpression(); |
|
|
return self[index].getMatchingExpression(); |
|
@ -569,11 +296,6 @@ Alternation.getMatchingExpression() { |
|
|
|
|
|
|
|
|
Action = Object.subtype(#Action); |
|
|
Action = Object.subtype(#Action); |
|
|
|
|
|
|
|
|
Action.match(stream, context, actions) { |
|
|
|
|
|
actions.push([action: self, context: context]); |
|
|
|
|
|
true; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
Action.getMatchingExpression() { |
|
|
Action.getMatchingExpression() { |
|
|
return `{ actions.push([action: @self, context: context]); @true; } |
|
|
return `{ actions.push([action: @self, context: context]); @true; } |
|
|
} |
|
|
} |
|
@ -600,14 +322,6 @@ Action.execute(context) { |
|
|
|
|
|
|
|
|
ParseTimeAction = Object.subtype(#ParseTimeAction); |
|
|
ParseTimeAction = Object.subtype(#ParseTimeAction); |
|
|
|
|
|
|
|
|
ParseTimeAction.match(stream, context, actions) { |
|
|
|
|
|
if(self.action.execute(context)) { |
|
|
|
|
|
true; |
|
|
|
|
|
} else { |
|
|
|
|
|
false; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
ParseTimeAction.getMatchingExpression() { |
|
|
ParseTimeAction.getMatchingExpression() { |
|
|
return `{ (@self.action).execute(context) }; |
|
|
return `{ (@self.action).execute(context) }; |
|
|
} |
|
|
} |
|
@ -616,12 +330,6 @@ ParseTimeAction.getMatchingExpression() { |
|
|
|
|
|
|
|
|
Assignment = Object.subtype(#Assignment); |
|
|
Assignment = Object.subtype(#Assignment); |
|
|
|
|
|
|
|
|
Assignment.match(stream, context, actions) { |
|
|
|
|
|
context.declareVariable(self.name); |
|
|
|
|
|
local innerContext = Context.new(outerContext: context, returnValueName: self.name).init(); |
|
|
|
|
|
self.rule.match(stream, innerContext, actions); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
Assignment.getMatchingExpression() { |
|
|
Assignment.getMatchingExpression() { |
|
|
return `{ |
|
|
return `{ |
|
|
context.declareVariable(@self.name); |
|
|
context.declareVariable(@self.name); |
|
@ -634,51 +342,16 @@ Assignment.getMatchingExpression() { |
|
|
|
|
|
|
|
|
RuleCall = Object.subtype(#RuleCall); |
|
|
RuleCall = Object.subtype(#RuleCall); |
|
|
|
|
|
|
|
|
RuleCall.match(stream, context, actions) { |
|
|
|
|
|
//if (rules[self.name] == nil) { print("Trying to call undefined rule: ", self.name, "\n"); exit(); } |
|
|
|
|
|
//print("calling rule ", self.name, " | ", stream.position, "\n"); |
|
|
|
|
|
local res = get(self.name).match(stream, context, actions); |
|
|
|
|
|
//print("matching rule ", self.name, ": ", res, "\n"); |
|
|
|
|
|
res; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
RuleCall.getMatchingExpression() { |
|
|
RuleCall.getMatchingExpression() { |
|
|
//print("Calling rule ", self.name, "\n"); |
|
|
|
|
|
//local res = `{get(@self.name).getMatchingExpression(@stream, @context, @actions).__eval__()}; |
|
|
|
|
|
|
|
|
|
|
|
//if (innerContext == nil) { |
|
|
|
|
|
`{ Invoke.new(self: self, method: @self.name, arguments: [stream, context, actions]).__eval__() }; |
|
|
|
|
|
//} else { |
|
|
|
|
|
// `{ self.rules[@self.name](stream, @innerContext, actions) }; |
|
|
|
|
|
//} |
|
|
|
|
|
|
|
|
|
|
|
//print("Pos ", stream.position, " | Called rule ", self.name, "\n"); |
|
|
|
|
|
|
|
|
`{ /*increaseCount(@self.name);*/ Invoke.new(self: self, method: @self.name, arguments: [stream, context, actions]).__eval__() }; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// NamespacedRuleCall |
|
|
// NamespacedRuleCall |
|
|
|
|
|
|
|
|
NamespacedRuleCall = Object.subtype(#NamespacedRuleCall); |
|
|
NamespacedRuleCall = Object.subtype(#NamespacedRuleCall); |
|
|
|
|
|
|
|
|
NamespacedRuleCall.match() { |
|
|
|
|
|
with(self.namespace); |
|
|
|
|
|
local res = get(self.name).match(stream, context, actions); |
|
|
|
|
|
without(); |
|
|
|
|
|
res; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
NamespacedRuleCall.getMatchingExpression(innerContext) { |
|
|
NamespacedRuleCall.getMatchingExpression(innerContext) { |
|
|
//return `{ |
|
|
|
|
|
// with(@self.namespace); |
|
|
|
|
|
// local res = get(@self.name).getMatchingExpression(@stream, @context, @actions).__eval__(); |
|
|
|
|
|
// without(); |
|
|
|
|
|
// res; |
|
|
|
|
|
//} |
|
|
|
|
|
//if (innerContext == nil) { |
|
|
|
|
|
`{ Invoke.new(self: @get(self.namespace), method: @self.name, arguments: [stream, context, actions]).__eval__() }; |
|
|
|
|
|
//} else { |
|
|
|
|
|
// `{ (@get(self.namespace)).rules[@self.name](stream, @innerContext, actions) }; |
|
|
|
|
|
//} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
`{ Invoke.new(self: @get(self.namespace), method: @self.name, arguments: [stream, context, actions]).__eval__() }; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
Definition = Object.subtype(#Definition); |
|
|
Definition = Object.subtype(#Definition); |
|
@ -692,12 +365,24 @@ Grammar.addRulesFromNamespace(namespace) { |
|
|
environment: nil, |
|
|
environment: nil, |
|
|
function: Lambda.new( |
|
|
function: Lambda.new( |
|
|
parameters: [#stream, #context, #actions], |
|
|
parameters: [#stream, #context, #actions], |
|
|
body: __namespaces__[namespace][key].getMatchingExpression().body |
|
|
|
|
|
|
|
|
body: __namespaces__[namespace][key].getMatchingExpression().body, |
|
|
|
|
|
name: key, |
|
|
|
|
|
parent: self |
|
|
) |
|
|
) |
|
|
) |
|
|
) |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//global functionCounts = []; |
|
|
|
|
|
// |
|
|
|
|
|
//increaseCount(function) { |
|
|
|
|
|
// if (!functionCounts.keys().contains(function)){ |
|
|
|
|
|
// functionCounts[function] = 1; |
|
|
|
|
|
// } else { |
|
|
|
|
|
// functionCounts[function]++; |
|
|
|
|
|
// } |
|
|
|
|
|
//} |
|
|
|
|
|
|
|
|
// ----- Grammar of Meta Language (Minimal) ----- |
|
|
// ----- Grammar of Meta Language (Minimal) ----- |
|
|
|
|
|
|
|
|
with(#HardCodedMeta); |
|
|
with(#HardCodedMeta); |
|
@ -1258,7 +943,9 @@ global stream = newStream(readfile("rawminproto.leg")); |
|
|
global context = Context.new(outerContext: nil).init(); |
|
|
global context = Context.new(outerContext: nil).init(); |
|
|
global actions = []; |
|
|
global actions = []; |
|
|
|
|
|
|
|
|
print("Matching rawminproto.leg : ", HardCodedPeg.grammar(stream, context, actions), "\n"); |
|
|
|
|
|
|
|
|
global match = HardCodedPeg.grammar(stream, context, actions); |
|
|
|
|
|
print("Matching rawminproto.leg : ", match, "\n"); |
|
|
|
|
|
if (!match) exit(); |
|
|
|
|
|
|
|
|
with(#metaLanguage); |
|
|
with(#metaLanguage); |
|
|
for (actionAndContext in actions) { |
|
|
for (actionAndContext in actions) { |
|
@ -1347,3 +1034,24 @@ compareGrammars(grammar1, grammar2) { |
|
|
|
|
|
|
|
|
compareGrammars(#peg, #pegCircular); |
|
|
compareGrammars(#peg, #pegCircular); |
|
|
compareGrammars(#metaLanguage, #metaLanguageCircular); |
|
|
compareGrammars(#metaLanguage, #metaLanguageCircular); |
|
|
|
|
|
|
|
|
|
|
|
//global functionCounts = []; |
|
|
|
|
|
global then = cputime(); |
|
|
|
|
|
|
|
|
|
|
|
global stream3 = newStream(readfile("grammar_parser.meta")); |
|
|
|
|
|
global context3 = Context.new(outerContext: nil).init(); |
|
|
|
|
|
global actions3 = []; |
|
|
|
|
|
|
|
|
|
|
|
while(!stream3.atEnd()) { |
|
|
|
|
|
metaLanguage.start(stream3, context3, actions3); |
|
|
|
|
|
for (actionAndContext in actions3) { |
|
|
|
|
|
actionAndContext.action.execute(actionAndContext.context); |
|
|
|
|
|
} |
|
|
|
|
|
//println(len(actions3)); |
|
|
|
|
|
//println(context3.variables.s); |
|
|
|
|
|
global actions3 = []; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
global now = cputime(); |
|
|
|
|
|
print("Parse time : ", now - then, "s\n"); |
|
|
|
|
|
//println(functionCounts); |