/// <summary> /// Replaces in the current statement, any references to local variables/arguments /// with their value. /// </summary> /// <param name="t"></param> /// <returns>A string with the expression after replacing the values.</returns> internal string ReplaceVarsWithValues(CommonTree t, CommonTokenStream cts, RoutineScope rs ) { Dictionary<string, StoreType> vars = rs.Variables; StringBuilder sb = new StringBuilder(); //foreach (IToken tok in cts.GetTokens(t.TokenStartIndex, t.TokenStopIndex)) for (int i = t.TokenStartIndex; i <= t.TokenStopIndex; i++) { IToken tok = cts.Get(i); IToken tok2 = null; StoreType st = null; if ((tok.Type == MySQL51Parser.AT1) && ((i + 1) <= t.TokenStopIndex) && ((tok2 = cts.Get(i + 1)).Type == MySQL51Parser.ID)) { string id = string.Format("@{0}", tok2.Text ); // TODO: What about qualified names line a.b? There's no way to add variables like that (until we support triggers and new.col). if (vars.TryGetValue(id, out st)) { i++; sb.Append(StoreType.WrapValue(st.Value)); } } else if (((tok.Type == MySQL51Parser.NEW) || ( Cmp( tok.Text, "old" ) == 0 )) && ((i + 1) <= t.TokenStopIndex) && ( cts.Get( i + 1 ).Type == MySQL51Parser.DOT ) && ( ( i + 2 ) <= t.TokenStopIndex ) && ( ( tok2 = cts.Get( i + 2 ) ).Type == MySQL51Parser.ID )) { string id = string.Format("{0}.{1}", tok.Text, tok2.Text ); if (vars.TryGetValue(id, out st)) { i += 2; sb.Append(StoreType.WrapValue(st.Value)); } else { sb.Append(tok.Text); } } else if (vars.TryGetValue(tok.Text, out st)) { sb.Append(StoreType.WrapValue(st.Value)); } else { sb.Append(tok.Text); } } return sb.ToString(); }
/// <summary> /// Concats a set of tokens from start to finish token indexes. /// </summary> /// <param name="sb"></param> /// <param name="cts"></param> /// <param name="StartTokenIndex"></param> /// <param name="EndTokenIndex"></param> private void ConcatTokens(StringBuilder sb, CommonTokenStream cts, int StartTokenIndex, int EndTokenIndex) { for (int i = StartTokenIndex; i <= EndTokenIndex; i++) { IToken tok = cts.Get(i); if (((i + 1) <= EndTokenIndex) && (cts.Get(i + 1).Type == MySQL51Parser.LPAREN) && ((i + 2) <= EndTokenIndex) && (cts.Get(i + 2).Type == MySQL51Parser.RPAREN)) { if (Cmp(tok.Text, "last_insert_id") == 0) { sb.Append(VAR_DBG_LAST_INSERT_ID); i += 2; continue; } else if (Cmp(tok.Text, "row_count") == 0) { sb.Append(VAR_DBG_ROW_COUNT); i += 2; continue; } else if (Cmp(tok.Text, "found_rows") == 0) { sb.Append(VAR_DBG_FOUND_ROWS); i += 2; continue; } } sb.Append(tok.Text); } }
/// <summary> /// Registers in the current scope all the session variables, so they can be properly emulated. /// </summary> /// <param name="cts"></param> /// <param name="vars"></param> private void ParseSessions(CommonTokenStream cts, Dictionary<string, StoreType> vars) { for( int i = 0; i < cts.Count; i++ ) { IToken tok = cts.Get(i); if ((tok.Type == MySQL51Parser.AT1) && ((i + 1) < cts.Count) && (cts.Get(i + 1).Type == MySQL51Parser.ID)) { if ((i > 0) && (cts.Get(i - 1).Type == MySQL51Parser.ID)) continue; string id = string.Format( "@{0}", cts.Get( i + 1 ).Text ); StoreType st; if( !vars.TryGetValue( id, out st ) ) { st = new StoreType() { Name = id, Type = "varchar", Value = DBNull.Value, VarKind = VarKindEnum.Session }; vars.Add(id, st ); } } } }
public static List<IToken> getPostHiddenTokens(ParserRuleReturnScope tree, CommonTokenStream rawTokens) { if (tree.Tree==null) { //I think this only happens with implied semicolons if (tree.Start is CommonToken && tree.Start!=null) { //I think we should always be on at least token 1. IToken currentTok=rawTokens.Get(((CommonToken)tree.Start).TokenIndex); //I go back one token if I am on a non-default channel token so that I can search forward for hidden tokens. if (currentTok.Channel!=Token.DEFAULT_CHANNEL) currentTok=rawTokens.Get(((CommonToken)tree.Start).TokenIndex-1); return getPostHiddenTokens(currentTok, rawTokens); } return null; } return getPostHiddenTokens(getLastTreeToken((CommonTree)tree.Tree), rawTokens); }
public static List<IToken> getHiddenTokens(IToken tok, CommonTokenStream rawTokens, bool crossLineBoundaries, bool filterNone) { List<IToken> results = new List<IToken>(); if (tok == null) return results; int currentTokenIndex = ((CommonToken)tok).TokenIndex - 1; bool seenCR = false; int tokensSinceLastCR = 0; while (currentTokenIndex >= 0) { IToken t = rawTokens.Get(currentTokenIndex); if (t.Channel == Token.DEFAULT_CHANNEL) break; if (t.Channel == CHANNEL_EOL || t.Channel == CHANNEL_SLCOMMENT) { if (!crossLineBoundaries) break; tokensSinceLastCR = 0; // if (t.getChannel()==ASCollectorParser.CHANNEL_SLCOMMENT) // tokensSinceLastCR++; seenCR = true; } else { tokensSinceLastCR++; } results.Insert(0, t); currentTokenIndex--; } //if we want all the hidden tokens (without any post-processing), then just return here if (filterNone) return results; //strip off tokens from previous line that had code on it if (seenCR && currentTokenIndex >= 0) { for (int i = 0; i < tokensSinceLastCR; i++) { results.RemoveAt(0); } } if (results.Count > 0 && currentTokenIndex >= 0) { //remove the first token if it contained a carriage return. The idea here is to not include the token that should //go with the previous line. Special case for first token of file if (results[0].Channel == CHANNEL_EOL || results[0].Channel == CHANNEL_SLCOMMENT) { results.RemoveAt(0); } else if (crossLineBoundaries) { //otherwise, remove whitespace tokens up to the first carriage return or all tokens while (results.Count > 0) { IToken t = results[0]; if (t.Channel == CHANNEL_EOL) break; results.RemoveAt(0); } } } //leave leading whitespace associated with the element // //now, strip off the leading whitespace // while (results.size()>0) // { // Token t=results.get(0); // if (t.getChannel()==ASCollectorParser.CHANNEL_EOL || t.getChannel()==ASCollectorParser.CHANNEL_WHITESPACE) // { // results.remove(0); // continue; // } // break; // } return results; }
// public static ASDocComment findPreviousComment(ParserRuleReturnScope t, CommonTokenStream rawTokens) { // // return findPreviousComment(getFirstTreeToken((CommonTree)t.getTree()), rawTokens); // } // // public static ASDocComment findPreviousComment(Token tok, CommonTokenStream rawTokens) // { // int currentTokenIndex=((CommonToken)tok).getTokenIndex()-1; //// List<Token> hiddenTokens=new ArrayList<Token>(); // // //collect all of the hidden tokens since the last non-whitespace token // while (currentTokenIndex>=0) // { // Token t=rawTokens.get(currentTokenIndex); // if (t.getChannel()==Token.DEFAULT_CHANNEL) // break; // // if (t.getType()==ASCollectorLexer.COMMENT_MULTILINE && t.getText().startsWith("/**")) // { // return new ASDocComment(t); // } //// hiddenTokens.add(t); // currentTokenIndex--; // } //// Collections.reverse(hiddenTokens); // return null; // } /*public static ASDocComment findCommentReverse(List<IToken> hiddenTokens) { int currentTokenIndex=hiddenTokens.Count-1; //collect all of the hidden tokens since the last non-whitespace token loop: while (currentTokenIndex>=0) { IToken t=hiddenTokens[currentTokenIndex]; switch (t.Channel) { case CHANNEL_MLCOMMENT: if (t.Text.StartsWith("/**")) return new ASDocComment(t); break loop; case CHANNEL_WHITESPACE: case CHANNEL_EOL: currentTokenIndex--; break; default: break loop; } } return null; }*/ public static List<IToken> getPostHiddenTokens(IToken tok, CommonTokenStream rawTokens) { List<IToken> results = new List<IToken>(); if (tok == null) return results; int currentTokenIndex = ((CommonToken)tok).TokenIndex + 1; while (currentTokenIndex < rawTokens.Count) { IToken t = rawTokens.Get(currentTokenIndex); if (t.Channel == Token.DEFAULT_CHANNEL) break; if (t.Channel == CHANNEL_EOL) break; if (t.Channel == CHANNEL_SLCOMMENT) { results.Add(t); break; } results.Add(t); currentTokenIndex++; } //walk backwards to remove whitespace tokens at the end of list for (int i = results.Count - 1; i >= 0; i--) { IToken t = results[i]; if (t.Channel == CHANNEL_WHITESPACE) results.RemoveAt(i); } return results; }
public static string getTokensString(CommonTokenStream tokens, int FirstToken, int LastToken) { if (LastToken - FirstToken < 0) return ""; string text = ""; for (int i = FirstToken; i <= LastToken; i++) { text = text + tokens.Get(i).Text; } return text; }
private void button1_Click(object sender, RoutedEventArgs e) { string s = Clipboard.GetText(); simpletikzLexer lex = new simpletikzLexer(new ANTLRStringStream(s)); CommonTokenStream tokens = new CommonTokenStream(lex); for (int i = 0; i < tokens.Count; i++) { string ds = tokens.Get(i).Text; ds = ds + "eee"; } simpletikzParser parser = new simpletikzParser(tokens); //tikzgrammarParser.expr_return r = simpletikzParser.tikzpath_return ret = parser.tikzpath(); //CommonTreeAdaptor adaptor = new CommonTreeAdaptor(); CommonTree t = (CommonTree)ret.Tree; MessageBox.Show(printTree(t,0)); }
/// <summary> /// Input an expression token in tree structure, return a single token representing the whole expression /// </summary> /// <param name="rootToken"></param> /// <param name="input"></param> /// <returns></returns> public static void CheckIsParsingComplete(CommonTokenStream tokens, CommonTree tree) { if (!(tokens.Index == tokens.Count - 2 && tokens.Get(tokens.Index).Text == "<EOF>" && tokens.Get(tokens.Index + 1).Text == "<EOF>")) { if (tokens.Index != tokens.Count - 1 || tokens.Get(tokens.Index).Text != "<EOF>") { throw new ParsingException("Unrecognized symbol.", tokens.Get(tokens.Index)); } } if (tree == null) { throw new ParsingException( "Please check your first line of your model (not comments), it may start with invalid symbols.", tokens.Get(0)); } }
private static string ToOriginalCode(int start, int end, int line, CommonTokenStream tokens) { if (start < 0 || end < 0) { return string.Empty; } StringBuilder buf = new StringBuilder(); for (int i = start; (i <= end) && (i < tokens.Count); i++) { IToken token = tokens.Get(i); // if we have added newlines, add them. if (token.Line > line) { int numberOfNewlines = token.Line - line; numberOfNewlines.Times(() => buf.Append(Environment.NewLine)); line = token.Line; // is this token indented to start off a new line? token.CharPositionInLine.Times(() => buf.Append(" ")); } buf.Append(token.Text); // is there another token on this line, and is there a space if (i + 1 < tokens.Count) { IToken nextToken = tokens.Get(i + 1); if (nextToken.Line == line) // on the same line { // add spaces if there are any int numberOfSpaces = nextToken.CharPositionInLine - (token.CharPositionInLine + token.Text.Length); numberOfSpaces.Times(() => buf.Append(" ")); } } } return buf.ToString(); }