public static void AddMacro(AstMacro node) { int n = node.mSrc.mPattern.Count; if (n == 0) { throw new Exception("a macro has to have at least one token in the source pattern"); } string s = node.mSrc.mPattern[n - 1].ToString(); if (s.Length < 1) { throw new Exception("illegal token in source pattern"); } if (s[0] == '$') { throw new Exception("last token in pattern can not be a variable"); } if (!mMacros.ContainsKey(s)) { mMacros.Add(s, new List <AstMacro>()); } mMacros[s].Add(node); }
public static void AddMacro(AstMacro node) { int n = node.mSrc.mPattern.Count; if (n == 0) throw new Exception("a macro has to have at least one token in the source pattern"); string s = node.mSrc.mPattern[n - 1].ToString(); if (s.Length < 1) throw new Exception("illegal token in source pattern"); if (s[0] == '$') throw new Exception("last token in pattern can not be a variable"); if (!mMacros.ContainsKey(s)) mMacros.Add(s, new List<AstMacro>()); mMacros[s].Add(node); }
public MacroMatch(AstMacro m) { mMacro = m; }
static public MacroMatch Create(AstMacro m, CatExpr fxns, int nPrevMatchPos, int nFxnIndex, int nSubExprSize) { if (nFxnIndex < 0) return null; if (nFxnIndex >= fxns.Count) return null; List<AstMacroTerm> pattern = m.mSrc.mPattern; MacroMatch match = new MacroMatch(m); int nFirst = nFxnIndex; int nLast = nFxnIndex; int nTokenIndex = pattern.Count - 1; // Start at the end of the pattern and move backwards comparing expressions while (nFirst > nPrevMatchPos) { Trace.Assert(nTokenIndex <= pattern.Count); Trace.Assert(nFirst >= 0); Trace.Assert(nLast >= nFirst); Trace.Assert(nTokenIndex < pattern.Count); // get the current sub-expression that we are evaluating CatExpr expr = fxns.GetRangeFromTo(nFirst, nLast); AstMacroTerm tkn = pattern[nTokenIndex]; bool bRecoverable = false; if (match.DoesTokenMatch(tkn, expr, out bRecoverable)) { // Check if we have matched the whole pattern if (nTokenIndex == 0) { match.mnFxnIndex = nFirst; match.mnFxnCount = (nFxnIndex - nFirst) + 1; return match; } // Go to the previous token nTokenIndex -= 1; // Adjust the sub-expression range nFirst -= 1; nLast = nFirst; } else { // Some failed matches (such as identifier names) can not be recovered from. if (!bRecoverable) return null; // Widen the sub-expression. nFirst -= 1; // Check if we have passed the limit of how big of a // sub-expression will be examined if (nLast - nFirst > nSubExprSize) return null; } } // The loop was finished and no match was found. return null; }
static public MacroMatch Create(AstMacro m, CatExpr fxns, int nPrevMatchPos, int nFxnIndex, int nSubExprSize) { if (nFxnIndex < 0) { return(null); } if (nFxnIndex >= fxns.Count) { return(null); } List <AstMacroTerm> pattern = m.mSrc.mPattern; MacroMatch match = new MacroMatch(m); int nFirst = nFxnIndex; int nLast = nFxnIndex; int nTokenIndex = pattern.Count - 1; // Start at the end of the pattern and move backwards comparing expressions while (nFirst > nPrevMatchPos) { Trace.Assert(nTokenIndex <= pattern.Count); Trace.Assert(nFirst >= 0); Trace.Assert(nLast >= nFirst); Trace.Assert(nTokenIndex < pattern.Count); // get the current sub-expression that we are evaluating CatExpr expr = fxns.GetRangeFromTo(nFirst, nLast); AstMacroTerm tkn = pattern[nTokenIndex]; bool bRecoverable = false; if (match.DoesTokenMatch(tkn, expr, out bRecoverable)) { // Check if we have matched the whole pattern if (nTokenIndex == 0) { match.mnFxnIndex = nFirst; match.mnFxnCount = (nFxnIndex - nFirst) + 1; return(match); } // Go to the previous token nTokenIndex -= 1; // Adjust the sub-expression range nFirst -= 1; nLast = nFirst; } else { // Some failed matches (such as identifier names) can not be recovered from. if (!bRecoverable) { return(null); } // Widen the sub-expression. nFirst -= 1; // Check if we have passed the limit of how big of a // sub-expression will be examined if (nLast - nFirst > nSubExprSize) { return(null); } } } // The loop was finished and no match was found. return(null); }