This commit is contained in:
kudzu_cp 2012-07-08 21:21:27 +00:00
parent 96ea92e02a
commit 9eeaffe4aa
2 changed files with 21 additions and 14 deletions

View file

@ -70,10 +70,15 @@ namespace Cosmos.Compiler.XSharp {
if (EmitXSharpCodeComments && !aLine.StartsWith("#")) { if (EmitXSharpCodeComments && !aLine.StartsWith("#")) {
mOutput.WriteLine("\t\t\tnew Comment(\"X#: " + aLine + "\");"); mOutput.WriteLine("\t\t\tnew Comment(\"X#: " + aLine + "\");");
} }
var xCode = mPatterns.GetCode(aLine); var xCode = mPatterns.GetCode(aLine);
if (xCode == null) {
throw new Exception("Parsing error: " + aLine);
}
foreach (var xLine in xCode) { foreach (var xLine in xCode) {
mOutput.WriteLine("\t\t\t" + xLine); mOutput.WriteLine("\t\t\t" + xLine);
} }
mOutput.WriteLine(); mOutput.WriteLine();
} }

View file

@ -464,28 +464,30 @@ namespace Cosmos.Compiler.XSharp {
}); });
} }
public List<string> GetCode(string aLine) { protected Pattern FindMatch(TokenList aTokens) {
var xParser = new Parser(aLine, false, false); int xHash = aTokens.GetPatternHashCode();
var xTokens = xParser.Tokens;
var xResult = new List<string>();
int xHash = xTokens.GetPatternHashCode();
// Get a list of matching hashes, but then we have to // Get a list of matching hashes, but then we have to
// search for exact pattern match because it is possible // search for exact pattern match because it is possible
// to have duplicate hashes. Hashes just provide us a quick way // to have duplicate hashes. Hashes just provide us a quick way
// to reduce the search. // to reduce the search.
var xPatterns = mPatterns.Where(q => q.Hash == xHash); foreach (var xPattern in mPatterns.Where(q => q.Hash == xHash)) {
Pattern xPattern = null; if (xPattern.Tokens.PatternMatches(aTokens)) {
foreach (var x in xPatterns) { return xPattern;
if (x.Tokens.PatternMatches(xTokens)) {
xPattern = x;
break;
} }
} }
if (xPattern == null) { return null;
throw new Exception("Token pattern not found: " + aLine);
} }
public List<string> GetCode(string aLine) {
var xParser = new Parser(aLine, false, false);
var xTokens = xParser.Tokens;
var xPattern = FindMatch(xTokens);
if (xPattern == null) {
return null;
}
var xResult = new List<string>();
xPattern.Code(xTokens, ref xResult); xPattern.Code(xTokens, ref xResult);
for(int i = 0; i < xResult.Count; i++) { for(int i = 0; i < xResult.Count; i++) {