diff --git a/source2/Compiler/Cosmos.XSharp/Generator.cs b/source2/Compiler/Cosmos.XSharp/Generator.cs index bc0f42ad5..84d407ab8 100644 --- a/source2/Compiler/Cosmos.XSharp/Generator.cs +++ b/source2/Compiler/Cosmos.XSharp/Generator.cs @@ -70,10 +70,15 @@ namespace Cosmos.Compiler.XSharp { if (EmitXSharpCodeComments && !aLine.StartsWith("#")) { mOutput.WriteLine("\t\t\tnew Comment(\"X#: " + aLine + "\");"); } + var xCode = mPatterns.GetCode(aLine); - foreach(var xLine in xCode) { + if (xCode == null) { + throw new Exception("Parsing error: " + aLine); + } + foreach (var xLine in xCode) { mOutput.WriteLine("\t\t\t" + xLine); } + mOutput.WriteLine(); } diff --git a/source2/Compiler/Cosmos.XSharp/TokenPatterns.cs b/source2/Compiler/Cosmos.XSharp/TokenPatterns.cs index 45d66d6dc..a700a43a9 100644 --- a/source2/Compiler/Cosmos.XSharp/TokenPatterns.cs +++ b/source2/Compiler/Cosmos.XSharp/TokenPatterns.cs @@ -464,28 +464,30 @@ namespace Cosmos.Compiler.XSharp { }); } - public List GetCode(string aLine) { - var xParser = new Parser(aLine, false, false); - var xTokens = xParser.Tokens; - var xResult = new List(); - int xHash = xTokens.GetPatternHashCode(); - + protected Pattern FindMatch(TokenList aTokens) { + int xHash = aTokens.GetPatternHashCode(); // Get a list of matching hashes, but then we have to // search for exact pattern match because it is possible // to have duplicate hashes. Hashes just provide us a quick way // to reduce the search. - var xPatterns = mPatterns.Where(q => q.Hash == xHash); - Pattern xPattern = null; - foreach (var x in xPatterns) { - if (x.Tokens.PatternMatches(xTokens)) { - xPattern = x; - break; + foreach (var xPattern in mPatterns.Where(q => q.Hash == xHash)) { + if (xPattern.Tokens.PatternMatches(aTokens)) { + return xPattern; } } + return null; + } + + public List GetCode(string aLine) { + var xParser = new Parser(aLine, false, false); + var xTokens = xParser.Tokens; + + var xPattern = FindMatch(xTokens); if (xPattern == null) { - throw new Exception("Token pattern not found: " + aLine); + return null; } + var xResult = new List(); xPattern.Code(xTokens, ref xResult); for(int i = 0; i < xResult.Count; i++) {