public string EditText(string text) { AntlrInputStream inputStream = new AntlrInputStream(text); ITokenSource lexer; IVisitorTree visitor; IChangeTokenSource editorTokens; lexer = new HtmlLexer(inputStream); visitor = new HtmlVisitorChangeAtributeValue(factoryNames, lexer.TokenFactory); editorTokens = new BaseHtmlEditTokens(visitor); lexer = editorTokens.Edit(lexer); visitor = new HtmlVisitorEditorScriptTag(factoryNames, lexer.TokenFactory, factoryEditor); editorTokens = new BaseHtmlEditTokens(visitor); lexer = editorTokens.Edit(lexer); visitor = new HtmlVisitorEditStyleTag(factoryNames, lexer.TokenFactory, factoryEditor); editorTokens = new BaseHtmlEditTokens(visitor); lexer = editorTokens.Edit(lexer); visitor = new HtmlVisitorAddSpace(lexer.TokenFactory); editorTokens = new BaseHtmlEditTokens(visitor); lexer = editorTokens.Edit(lexer); CommonTokenStream cs = new CommonTokenStream(lexer); cs.Fill(); return(cs.GetText()); }
private static String RewriteScripts(IList <UniformPair <int?> > ranges, CommonTokenStream tokens) { if (ranges.IsEmpty()) { return(tokens.GetText()); } var writer = new StringWriter(); var rangeIndex = 0; UniformPair <int?> current = ranges[rangeIndex]; for (var i = 0; i < tokens.Size; i++) { var t = tokens.Get(i); if (t.Type == EsperEPL2GrammarLexer.Eof) { break; } if (i < current.First) { writer.Write(t.Text); } else if (i == current.First) { writer.Write(t.Text); writer.Write("'"); } else if (i == current.Second) { writer.Write("'"); writer.Write(t.Text); rangeIndex++; if (ranges.Count > rangeIndex) { current = ranges[rangeIndex]; } else { current = new UniformPair <int?>(-1, -1); } } else if (t.Type == EsperEPL2GrammarParser.SL_COMMENT || t.Type == EsperEPL2GrammarParser.ML_COMMENT) { WriteCommentEscapeSingleQuote(writer, t); } else { if (t.Type == EsperEPL2GrammarParser.QUOTED_STRING_LITERAL && i > current.First && i < current.Second) { writer.Write("\\'"); writer.Write(t.Text.Substring(1, t.Text.Length - 2)); writer.Write("\\'"); } else { writer.Write(t.Text); } } } return(writer.ToString()); }
public static ASTWalkException From( string message, CommonTokenStream tokenStream, RuleContext parseTree) { return new ASTWalkException(message + " in text '" + tokenStream.GetText(parseTree) + "'"); }
public static void Reconstruct(IParseTree tree, CommonTokenStream stream) { if (tree as TerminalNodeImpl != null) { TerminalNodeImpl tok = tree as TerminalNodeImpl; Interval interval = tok.SourceInterval; var inter = stream.GetHiddenTokensToLeft(tok.Symbol.TokenIndex); if (inter != null) { foreach (var t in inter) { System.Console.Write(t.Text); } } var s = stream.GetText(interval); System.Console.Write(s); } else { for (int i = 0; i < tree.ChildCount; ++i) { var c = tree.GetChild(i); Reconstruct(c, stream); } } }
public string EditText(string text) { AntlrInputStream inputStream = new AntlrInputStream(text); ITokenSource lexer; IVisitorTree visitor; IChangeTokenSource editorTokens; lexer = new CssLexer(inputStream); visitor = new CssVisitorEditNameSelector(factoryNames, lexer.TokenFactory); editorTokens = new BaseCssEditTokens(visitor); lexer = editorTokens.Edit(lexer); CommonTokenStream cs = new CommonTokenStream(lexer); cs.Fill(); return(cs.GetText()); }
public override void ExitMacroInstruction([NotNull] GoToParser.MacroInstructionContext context) { base.ExitMacroInstruction(context); var macroContext = context.macro(); var name = macroContext.macroName().GetText(); var @params = macroContext.macroParams(); if (_macrosBodies.TryGetValue(name, out GoToParser.MacroBodyContext body)) { var macroBody = _tokenStream.GetText(body.Start, body.Stop); var macroParams = _macrosParams[name]; var macroReplaced = macroBody; for (var i = 0; i < macroParams.ChildCount; i++) { var sourceParam = macroParams.GetChild(i); var targetParam = @params.GetChild(i); macroReplaced = macroReplaced.Replace(sourceParam.GetText(), targetParam.GetText()); _rewrittenTokenStream.Replace(context.Start, context.Stop, macroReplaced); } } }
public static MyPair WalkExpressionDecl( EsperEPL2GrammarParser.ExpressionDeclContext ctx, IList<string> scriptBodies, IDictionary<ITree, ExprNode> astExprNodeMap, CommonTokenStream tokenStream) { var name = ctx.name.Text; if (ctx.alias != null) { if (!ctx.alias.Text.ToLowerInvariant().Trim().Equals("alias")) { throw ASTWalkException.From( "For expression alias '" + name + "' expecting 'alias' keyword but received '" + ctx.alias.Text + "'"); } if (ctx.columnList() != null) { throw ASTWalkException.From( "For expression alias '" + name + "' expecting no parameters but received '" + tokenStream.GetText(ctx.columnList()) + "'"); } if (ctx.expressionDef() != null && ctx.expressionDef().expressionLambdaDecl() != null) { throw ASTWalkException.From( "For expression alias '" + name + "' expecting an expression without parameters but received '" + tokenStream.GetText(ctx.expressionDef().expressionLambdaDecl()) + "'"); } if (ctx.expressionDef().stringconstant() != null) { throw ASTWalkException.From("For expression alias '" + name + "' expecting an expression but received a script"); } var node = ASTExprHelper.ExprCollectSubNodes(ctx, 0, astExprNodeMap)[0]; var alias = ctx.name.Text; var expressionUnmap = StatementSpecMapper.Unmap(node); var decl = new ExpressionDeclItem(alias, new string[0], true); decl.OptionalSoda = expressionUnmap; return new MyPair(decl, null); } if (ctx.expressionDef().stringconstant() != null) { var expressionText = scriptBodies.DeleteAt(0); var parameters = ASTUtil.GetIdentList(ctx.columnList()); var optionalReturnType = ctx.classIdentifier() == null ? null : ASTUtil.UnescapeClassIdent(ctx.classIdentifier()); var optionalReturnTypeArray = ctx.array != null; var optionalDialect = ctx.expressionDialect() == null ? null : ctx.expressionDialect().d.Text; var optionalEventTypeName = ASTTypeExpressionAnnoHelper.ExpectMayTypeAnno(ctx.typeExpressionAnnotation(), tokenStream); var script = new ExpressionScriptProvided( name, expressionText, parameters.ToArray(), optionalReturnType, optionalReturnTypeArray, optionalEventTypeName, optionalDialect); return new MyPair(null, script); } var ctxexpr = ctx.expressionDef(); var inner = ASTExprHelper.ExprCollectSubNodes(ctxexpr.expression(), 0, astExprNodeMap)[0]; IList<string> parametersNames = new EmptyList<string>(); var lambdactx = ctxexpr.expressionLambdaDecl(); if (ctxexpr.expressionLambdaDecl() != null) { parametersNames = ASTLambdaHelper.GetLambdaGoesParams(lambdactx); } var expression = StatementSpecMapper.Unmap(inner); var expr = new ExpressionDeclItem(name, parametersNames.ToArray(), false); expr.OptionalSoda = expression; return new MyPair(expr, null); }
public static Pair <ExpressionDeclItem, ExpressionScriptProvided> WalkExpressionDecl( EsperEPL2GrammarParser.ExpressionDeclContext ctx, IList <String> scriptBodies, IDictionary <ITree, ExprNode> astExprNodeMap, CommonTokenStream tokenStream) { var name = ctx.name.Text; if (ctx.alias != null) { if (ctx.alias.Text.ToLower().Trim() != "alias") { throw ASTWalkException.From("For expression alias '" + name + "' expecting 'alias' keyword but received '" + ctx.alias.Text + "'"); } if (ctx.columnList() != null) { throw ASTWalkException.From("For expression alias '" + name + "' expecting no parameters but received '" + tokenStream.GetText(ctx.columnList()) + "'"); } if (ctx.expressionDef() != null && ctx.expressionDef().expressionLambdaDecl() != null) { throw ASTWalkException.From("For expression alias '" + name + "' expecting an expression without parameters but received '" + tokenStream.GetText(ctx.expressionDef().expressionLambdaDecl()) + "'"); } if (ctx.expressionDef().stringconstant() != null) { throw ASTWalkException.From("For expression alias '" + name + "' expecting an expression but received a script"); } var exprNode = ASTExprHelper.ExprCollectSubNodes(ctx, 0, astExprNodeMap)[0]; var alias = ctx.name.Text; var decl = new ExpressionDeclItem(alias, Collections.GetEmptyList <String>(), exprNode, true); return(new Pair <ExpressionDeclItem, ExpressionScriptProvided>(decl, null)); } if (ctx.expressionDef().stringconstant() != null) { var expressionText = scriptBodies[0]; scriptBodies.RemoveAt(0); var parameters = ASTUtil.GetIdentList(ctx.columnList()); var optionalReturnType = ctx.classIdentifier() == null ? null : ASTUtil.UnescapeClassIdent(ctx.classIdentifier()); var optionalReturnTypeArray = ctx.array != null; var optionalDialect = ctx.expressionDialect() == null ? null : ctx.expressionDialect().d.Text; var script = new ExpressionScriptProvided( name, expressionText, parameters, optionalReturnType, optionalReturnTypeArray, optionalDialect); return(new Pair <ExpressionDeclItem, ExpressionScriptProvided>(null, script)); } var ctxexpr = ctx.expressionDef(); var inner = ASTExprHelper.ExprCollectSubNodes(ctxexpr.expression(), 0, astExprNodeMap)[0]; var parametersNames = Collections.GetEmptyList <string>(); var lambdactx = ctxexpr.expressionLambdaDecl(); if (ctxexpr.expressionLambdaDecl() != null) { parametersNames = ASTLibFunctionHelper.GetLambdaGoesParams(lambdactx); } var expr = new ExpressionDeclItem(name, parametersNames, inner, false); return(new Pair <ExpressionDeclItem, ExpressionScriptProvided>(expr, null)); }