public ParsingTask Process(ParsingTask input) { if (string.IsNullOrEmpty(input.ParsingInput)) { input.Errors.Add("Input string is empty"); _canContinue = false; return(input); } QLParser parser = SetupParser(input.ParsingInput); var errorListener = new ErrorListener(); parser.AddErrorListener(errorListener); var visitor = new ParseTreeVisitor(new OperatorFactory(new ValueFactory())); input.Ast = visitor.Visit(parser.form()); errorListener.Errors.ForEach(x => input.Errors.Add(x)); _canContinue = !errorListener.Errors.Any(); return(input); }
public Program Compile(string sourceCode) { var inputStream = new AntlrInputStream(sourceCode); var lexer = new MonoLangLexer(inputStream); var tokens = new CommonTokenStream(lexer); var parser = new MonoLangParser(tokens); var errorListener = new ErrorListener <IToken>(); var programBuilder = new MonoLangProgramBuilder(); parser.AddParseListener(programBuilder); parser.AddErrorListener(errorListener); var tree = parser.prog(); if (errorListener.had_error) { throw new ParserException(); } //Debug.WriteLine(Output.OutputTokens(tokens.GetTokens())); return(programBuilder.Program); }
static void Main(string[] args) { List <string> options = new List <string>(); List <string> arguments = new List <string>(); string ast_output_file = null; CommandLine.Parser.Default.ParseArguments <Options>(args) .WithParsed <Options>(o => { arguments = o.CsharpFiles.ToList(); ast_output_file = o.AstOutFile; }) .WithNotParsed(a => { System.Console.Error.WriteLine(a); }); Runtime.Redirect r = new Runtime.Redirect(ast_output_file); foreach (var file_name in arguments) { var code_as_string = File.ReadAllText(file_name); var input = new AntlrInputStream(code_as_string); var lexer = new CSharpLexer(input); var tokens = new CommonTokenStream(lexer); var parser = new CSharpParser(tokens); var listener = new ErrorListener <IToken>(); parser.AddErrorListener(listener); CSharpParser.Compilation_unitContext tree = parser.compilation_unit(); if (listener.had_error) { return; } var sb = new StringBuilder(); var ser = new Runtime.AstHelpers(); ser.ParenthesizedAST(sb, file_name, tree, tokens); System.Console.Error.WriteLine(sb.ToString()); } r.Dispose(); }
public static bool TryParse(string text, out SqlQuery sqlQuery) { if (text == null) { throw new ArgumentNullException(nameof(text)); } AntlrInputStream str = new AntlrInputStream(text); sqlLexer lexer = new sqlLexer(str); CommonTokenStream tokens = new CommonTokenStream(lexer); sqlParser parser = new sqlParser(tokens) { ErrorHandler = ThrowExceptionOnErrors.Singleton, }; ErrorListener <IToken> listener = new ErrorListener <IToken>(parser, lexer, tokens); parser.AddErrorListener(listener); sqlParser.ProgramContext programContext; try { programContext = parser.program(); } catch (Exception) { sqlQuery = default; return(false); } if (listener.hadError) { sqlQuery = default; return(false); } sqlQuery = (SqlQuery)CstToAstVisitor.Singleton.Visit(programContext); return(true); }
public Implicit.DAEIDescription CompileDAEImplicit(string text) { compilerErrors = new List <ErrorMessage>(); parameters = new Dictionary <string, double>(); constants = new Dictionary <string, double>(); variables = new Dictionary <string, Variable>(); FunctionTable.Init(); //variables.Add("t", new Variable { Name = "t", InitialValue = 0.0, Initialized = true, VarType = Variable.Type.Algebraic, Count = 0 }); AntlrInputStream inputStream = new AntlrInputStream(text); DAEImplicitGrammarLexer eqLexer = new DAEImplicitGrammarLexer(inputStream); eqLexer.RemoveErrorListeners(); ErrorListener <int> lexerListener = new ErrorListener <int>(); eqLexer.AddErrorListener(lexerListener); CommonTokenStream commonTokenStream = new CommonTokenStream(eqLexer); DAEImplicitGrammarParser eqParser = new DAEImplicitGrammarParser(commonTokenStream); ErrorListener <IToken> parserListener = new ErrorListener <IToken>(); eqParser.RemoveErrorListeners(); eqParser.AddErrorListener(parserListener); DAEImplicitGrammarParser.CompileUnitContext eqContext = eqParser.compileUnit(); compilerErrors = lexerListener.GetErrors(); if (compilerErrors.Count > 0) { throw new CompilerException(compilerErrors, "Ошибки лексера"); } compilerErrors = parserListener.GetErrors(); if (compilerErrors.Count > 0) { throw new CompilerException(compilerErrors, "Ошибки парсера"); } DAEImplicitGrammarVisitor visitor = new DAEImplicitGrammarVisitor(); ASTNode root = visitor.VisitCompileUnit(eqContext); return(CompileDAEImplicit((RootNode)root)); }
private FieldTypeDecimal DecimalTypeFromContext(DmolParser.TypeContext context) { var precision = 18; var scale = 2; var parameters = context.typeParameters()?.typeParameter() ?? Array.Empty <TypeParameterContext>(); if (parameters.Length > 0) { if (parameters[0].INTEGER() != null) { precision = Convert.ToInt32(parameters[0].INTEGER().GetText()); } else { ErrorListener.SemanticError(context, "The parameter must be an INTEGER."); return(null); } } if (parameters.Length > 1) { if (parameters[1].INTEGER() != null) { scale = Convert.ToInt32(parameters[1].INTEGER().GetText()); } else { ErrorListener.SemanticError(context, "The parameter must be an INTEGER."); return(null); } } if (parameters.Length > 2) { var typeName = context.typeName().DECIMAL().GetText(); ErrorListener.SemanticError(context, $"The '{typeName}' type only accepts two parameters."); return(null); } return(new FieldTypeDecimal(precision, scale)); }
internal void ParseRoute(string routeString, out IMessageSource messageSource, out string condition, out Endpoint endpoint) { var errorListener = new ErrorListener(); var input = new AntlrInputStream(routeString); var lexer = new RouteLexer(input); var tokens = new CommonTokenStream(lexer); var parser = new RouteParser(tokens); parser.RemoveErrorListeners(); parser.AddErrorListener(errorListener); IParseTree tree = parser.route(); errorListener.Validate(); var walker = new ParseTreeWalker(); var listener = new RouteParserListener(this.endpointFactory); walker.Walk(listener, tree); condition = listener.Condition ?? DefaultCondition; messageSource = CustomMessageSource.Create(listener.Source); endpoint = listener.Endpoint; }
private async Task <bool> RefreshTokenAsync() { var request = GetRequestMessage(GetUriNoVersion("oauth/token"), mediaType: MediaTypeFormUrlEncoded); var parameters = new Dictionary <string, string> { { "client_id", WebApiSetting.ClientId }, { "redirect_uri", RedirectUri }, { "grant_type", "refresh_token" }, { "refresh_token", WebApiSetting.RefreshToken }, }; request.Content = new StringContent(GetFormUrlEncodedEntity(parameters), Encoding.UTF8, MediaTypeFormUrlEncoded); var response = await client.SendAsync(request); var content = await response.Content.ReadAsStringAsync(); if (!response.IsSuccessStatusCode) { ErrorListener?.Invoke($"トークンのリフレッシュに失敗しました。{Environment.NewLine}{response}{Environment.NewLine}{content}"); return(false); } return(ParseTokenContent(content)); }
private static IParseTree AntlrParseTemplates(string text, string id) { if (string.IsNullOrEmpty(text)) { return(null); } var input = new AntlrInputStream(text); var lexer = new LGFileLexer(input); lexer.RemoveErrorListeners(); var tokens = new CommonTokenStream(lexer); var parser = new LGFileParser(tokens); parser.RemoveErrorListeners(); var listener = new ErrorListener(id); parser.AddErrorListener(listener); parser.BuildParseTree = true; return(parser.file()); }
static public void RunModel(string inputText, ref List <ErrorMessage> errorList, ref List <string> output) { AntlrInputStream inputStream = new AntlrInputStream(inputText); PowerModel.NewModel.ModelGrammarLexer modelLexer = new PowerModel.NewModel.ModelGrammarLexer(inputStream); ErrorListener <int> lexerErrorListener = new ErrorListener <int>(); modelLexer.RemoveErrorListeners(); modelLexer.AddErrorListener(lexerErrorListener); CommonTokenStream commonTokenStream = new CommonTokenStream(modelLexer); PowerModel.NewModel.ModelGrammarParser modelParser = new PowerModel.NewModel.ModelGrammarParser(commonTokenStream); ErrorListener <IToken> parserErrorListener = new ErrorListener <IToken>(); modelParser.RemoveErrorListeners(); modelParser.AddErrorListener(parserErrorListener); PowerModel.NewModel.ModelGrammarParser.ModelContext modelContext = modelParser.model(); errorList.AddRange(lexerErrorListener.GetErrors()); errorList.AddRange(parserErrorListener.GetErrors()); if (errorList.Count > 0) { return; } PowerModel.NewModel.Grammar.Visitor visitor = new PowerModel.NewModel.Grammar.Visitor(); PowerModel.NewModel.Grammar.Node root = visitor.VisitModel(modelContext); var model = PowerModel.NewModel.ModelInterpreter.GetInstanse().Generate((PowerModel.NewModel.Grammar.ModelNode)root, ref errorList, ref output); if (errorList.Count > 0) { return; } string solverOutput = model.Solve(); //string solverOutput = model.GetEquations(); output.Add(solverOutput); }
/// <summary> /// 認証コードより、access_token / refresh_token を取得する処理 /// </summary> /// <param name="authorizationCode"></param> /// <returns></returns> private async Task <bool> GetTokenAsync(string authorizationCode) { var request = GetRequestMessage(GetUri("Auth/Token"), MediaTypeFormUrlEncoded); var parameters = new Dictionary <string, string> { { "grant_type", "authorization_code" }, { "code", authorizationCode }, { "client_id", WebApiSetting.ClientId }, { "client_secret", WebApiSetting.ClientSecret }, { "redirect_uri", RedirectUri } }; request.Content = new StringContent(GetFormUrlEncodedEntity(parameters), Encoding.UTF8, MediaTypeFormUrlEncoded); var response = await client.SendAsync(request); var content = await response.Content.ReadAsStringAsync(); if (!response.IsSuccessStatusCode) { ErrorListener?.Invoke($"トークンの取得に失敗しました。{Environment.NewLine}{response}{Environment.NewLine}{content}"); return(false); } return(ParseTokenContent(content)); }
/// <summary>連携された 請求ID に対しての 入金ステータス更新</summary> /// <param name="ids">idの配列</param> /// <param name="isMatched">true:2:消込済, false:0:未設定</param> /// <returns>一件でもエラーがあればエラー</returns> public async Task <bool> PatchPaymentAsync(IEnumerable <string> ids, bool isMatched) { var value = new MFModels.UpdateItem(isMatched); var jsonContent = new StringContent(value.ConvertToJson(), Encoding.UTF8, MediaTypeJson); Func <string, HttpRequestMessage> getRequestMessage = (id) => GetRequestMessage(GetUri($"billings/{id}/billing_status/payment"), new HttpMethod("PATCH"), initializer: request => request.Content = jsonContent); var result = true; try { foreach (var id in ids) { var response = await SendInnerAsync(() => getRequestMessage(id)); if (!response.IsSuccessStatusCode) { var content = await response.Content.ReadAsStringAsync(); ErrorListener?.Invoke(content); result = false; break; } if (id != ids.Last()) { await Task.Delay(TimeSpan.FromSeconds(RequestIntervalSecondsForDefaultApi)); } } } catch (Exception ex) { ErrorListener?.Invoke(ex.ToString()); } return(result); }
static void Main(string[] args) { bool show_tree = false; bool show_tokens = false; bool old = false; bool two_byte = false; string file_name = null; string input = null; System.Text.Encoding encoding = null; for (int i = 0; i < args.Length; ++i) { if (args[i].Equals("-tokens")) { show_tokens = true; continue; } else if (args[i].Equals("-two-byte")) { two_byte = true; continue; } else if (args[i].Equals("-old")) { old = true; continue; } else if (args[i].Equals("-tree")) { show_tree = true; continue; } else if (args[i].Equals("-input")) { input = args[++i]; } else if (args[i].Equals("-file")) { file_name = args[++i]; } else if (args[i].Equals("-encoding")) { ++i; encoding = Encoding.GetEncoding( args[i], new EncoderReplacementFallback("(unknown)"), new DecoderReplacementFallback("(error)")); if (encoding == null) { throw new Exception(@"Unknown encoding. Must be an Internet Assigned Numbers Authority (IANA) code page name. https://www.iana.org/assignments/character-sets/character-sets.xhtml"); } } } ICharStream str = null; if (input == null && file_name == null) { str = CharStreams.fromStream(System.Console.OpenStandardInput()); } else if (input != null) { str = CharStreams.fromString(input); } else if (file_name != null) { if (two_byte) { str = new TwoByteCharStream(file_name); } else if (old) { FileStream fs = new FileStream(file_name, FileMode.Open); str = new Antlr4.Runtime.AntlrInputStream(fs); } else if (encoding == null) { str = CharStreams.fromPath(file_name); } else { str = CharStreams.fromPath(file_name, encoding); } } var lexer = new JavaScriptLexer(str); if (show_tokens) { StringBuilder new_s = new StringBuilder(); for (int i = 0; ; ++i) { var ro_token = lexer.NextToken(); var token = (CommonToken)ro_token; token.TokenIndex = i; new_s.AppendLine(token.ToString()); if (token.Type == Antlr4.Runtime.TokenConstants.EOF) { break; } } System.Console.Error.WriteLine(new_s.ToString()); lexer.Reset(); } var tokens = new CommonTokenStream(lexer); var parser = new JavaScriptParser(tokens); var listener_lexer = new ErrorListener <int>(); var listener_parser = new ErrorListener <IToken>(); lexer.AddErrorListener(listener_lexer); parser.AddErrorListener(listener_parser); DateTime before = DateTime.Now; var tree = parser.program(); DateTime after = DateTime.Now; System.Console.Error.WriteLine("Time: " + (after - before)); if (listener_lexer.had_error || listener_parser.had_error) { System.Console.Error.WriteLine("Parse failed."); } else { System.Console.Error.WriteLine("Parse succeeded."); } if (show_tree) { System.Console.Error.WriteLine(tree.ToStringTree(parser)); } System.Environment.Exit(listener_lexer.had_error || listener_parser.had_error ? 1 : 0); }
/// <summary> /// Parses an LSL list from a string and returns the simple expressions it contains as an enumerable. /// <remarks> /// Take note that parsing wont start to occur until you begin enumerating the returned value. /// </remarks> /// </summary> /// <param name="list">The string containing the list.</param> /// <param name="parsingFlags">Optional parsing flags.</param> /// <returns>An enumerable of <see cref="ILSLListExpr"/> nodes parsed from the list string.</returns> /// <exception cref="LSLListParserOptionsConstraintException"> /// When an /// <see cref="LSLListParsingFlags" /> constraint is violated. /// </exception> /// <exception cref="LSLListParserSyntaxException"> /// Rotations must contain only literal values to be parsed, and cannot contain these types of expressions: rotations, /// vectors, lists or strings. /// or /// Vectors must contain only literal values to be parsed, and cannot contain these types of expressions: rotations, /// vectors, lists or strings. /// or /// Lists cannot contain other lists. /// or /// Cast expressions can only be used to specify that a list element is a 'key' and not a 'string' /// or /// Encountered an un-parseable expression in the list, only literal values and possibly variable names are acceptable /// when parsing. /// </exception> public static IEnumerable <ILSLListExpr> ParseList(string list, LSLListParsingFlags parsingFlags = LSLListParsingFlags.None) { var strm = new AntlrInputStream(new StringReader(list)); var lex = new LSLLexer(strm); var parse = new LSLParser(new CommonTokenStream(lex)); parse.ErrorListeners.Clear(); lex.ErrorListeners.Clear(); var errorListener = new ErrorListener(); parse.AddErrorListener(errorListener); parse.AddErrorListener(errorListener); var production = parse.listLiteral(); if (production.expression_list == null) { throw new LSLListParserSyntaxException("List rule missing expression list, parsing error"); } var listProduction = production.expression_list.expressionList(); if (listProduction == null) { yield break; } foreach (var child in EnumerateExpressionList(listProduction)) { var atomToken = child as LSLParser.Expr_AtomContext; var castExpression = child as LSLParser.Expr_TypeCastContext; var negateOrPositive = child as LSLParser.Expr_PrefixOperationContext; if (atomToken != null) { var maybeBasic = BasicAtomToExpr(atomToken); if (maybeBasic != null) { yield return(maybeBasic); } if (atomToken.variable != null) { if ((parsingFlags & LSLListParsingFlags.AllowVariableReferencesInList) == LSLListParsingFlags.AllowVariableReferencesInList) { yield return(new LSLListStringExpr(atomToken.GetText())); } else { throw new LSLListParserOptionsConstraintException( "Variable references are not allowed in the list."); } } if (atomToken.rotation_literal != null) { yield return(ListExpressionFromRotation(parsingFlags, atomToken)); } if (atomToken.vector_literal != null) { yield return(ListExpressionFromVector(parsingFlags, atomToken)); } if (atomToken.list_literal != null) { throw new LSLListParserSyntaxException("Lists cannot contain other lists."); } } else if (castExpression != null) { var stringLiteral = castExpression.expr_rvalue as LSLParser.Expr_AtomContext; if (stringLiteral != null && stringLiteral.string_literal != null && castExpression.cast_type.Text == "key") { yield return(new LSLListKeyExpr(stringLiteral.GetText())); } else { throw new LSLListParserSyntaxException( "Cast expressions can only be used to specify that a list element is a 'key' and not a 'string'"); } } else if (negateOrPositive != null) { var floatOrInt = negateOrPositive.expr_rvalue as LSLParser.Expr_AtomContext; var operation = negateOrPositive.operation.Text; var validType = floatOrInt != null && (floatOrInt.float_literal != null || floatOrInt.integer_literal != null); if (validType && operation == "-" || operation == "+") { yield return(BasicAtomToExpr(floatOrInt, operation)); } else { throw new LSLListParserSyntaxException( string.Format( "The Negative and Positive prefix operator can only be used on Floats and Integer list elements, operator '{0}' is not valid.", operation)); } } else { throw new LSLListParserSyntaxException( "Encountered an un-parseable expression in the list, only LSL literals and possibly variable names are acceptable list elements."); } } }
/// <summary> /// Set the error event listener for the TransformerFactory, which /// is used for the processing of transformation instructions, /// and not for the transformation itself. /// </summary> abstract public void setErrorListener(ErrorListener @listener);
internal PrefSQLModel GetPrefSqlModelFromPreferenceSql(string preferenceSql) { PrefSQLParser parser = new PrefSQLParser(new CommonTokenStream(new PrefSQLLexer(new AntlrInputStream(preferenceSql)))); // An error listener helps to return detailed parser syntax errors ErrorListener listener = new ErrorListener(); parser.AddErrorListener(listener); IParseTree tree = parser.parse(); // PrefSQLModel is built during the visit of the parse tree SQLVisitor visitor = new SQLVisitor {IsNative = _skylineType.IsNative()}; visitor.Visit(tree); PrefSQLModel prefSql = visitor.Model; if (prefSql != null) { prefSql.OriginalPreferenceSql = preferenceSql; } return prefSql; }
public override sealed string ToString() { if (Engine.ResultTemplate == null) return base.ToString(); Errors.Clear(); try { _results = new ResultDB(_seed); var template = new Template(Engine.ResultTemplate.Replace(".$", "$").Replace("$this$", Engine.Name), '$', '$'); template.Add("this", this); template.Add("dyce", _results); template.Group.RegisterRenderer(typeof(object), new BasicFormatRenderer()); StringWriter writer = new StringWriter(); ErrorListener listener = new ErrorListener(); template.Write(writer, listener); if (listener.HasErrors) Errors.AddRange(listener.Errors); if (Errors.Any()) return Errors.JoinToString(" / "); if (_results.Results.Any()) RaisePropertyChanged(() => SubResults); return PostProcess(writer.ToString()); } catch (Exception ex) { Errors.Add(new ResultError(ex, Engine.ResultTemplate)); return Errors.JoinToString(" / "); } }
public static LogicalExpression Compile(string expression, bool nocache) { LogicalExpression logicalExpression = null; if (_cacheEnabled && !nocache) { try { Rwl.AcquireReaderLock(Timeout.Infinite); if (_compiledExpressions.ContainsKey(expression)) { Trace.TraceInformation("Expression retrieved from cache: " + expression); var wr = _compiledExpressions[expression]; logicalExpression = wr.Target as LogicalExpression; if (wr.IsAlive && logicalExpression != null) { return logicalExpression; } } } finally { Rwl.ReleaseReaderLock(); } } if (logicalExpression == null) { var lexer = new NCalc2Lexer(new AntlrInputStream(expression)); var parser = new NCalc2Parser(new CommonTokenStream(lexer)); var errorListener = new ErrorListener(); parser.AddErrorListener(errorListener); logicalExpression = parser.ncalc().retValue; if (errorListener.Errors.Any()) { throw new EvaluationException(string.Join(Environment.NewLine, errorListener.Errors.ToArray())); } if (_cacheEnabled && !nocache) { try { Rwl.AcquireWriterLock(Timeout.Infinite); _compiledExpressions[expression] = new WeakReference(logicalExpression); } finally { Rwl.ReleaseWriterLock(); } CleanCache(); Trace.TraceInformation("Expression added to cache: " + expression); } } return logicalExpression; }
/// <summary> /// Set the error event listener for the TransformerFactory, which /// is used for the processing of transformation instructions, /// and not for the transformation itself. /// </summary> abstract public void setErrorListener(ErrorListener @listener);
//=================================================================== public void Send <TRsp>(uint pid, object req, uint connId, ProtocolListener listener, ErrorListener errorListener = null) { Send(pid, req, connId, typeof(TRsp), listener, errorListener); }
static Antlr4.Runtime.Tree.IParseTree DoParse(string input) { bool show_tree = false; bool show_tokens = false; bool old = false; bool two_byte = false; string file_name = null; System.Text.Encoding encoding = null; ICharStream str = null; if (input == null && file_name == null) { str = CharStreams.fromStream(System.Console.OpenStandardInput()); } else if (input != null) { str = CharStreams.fromString(input); } else if (file_name != null) { if (two_byte) { str = new TwoByteCharStream(file_name); } else if (old) { FileStream fs = new FileStream(file_name, FileMode.Open); str = new Antlr4.Runtime.AntlrInputStream(fs); } else if (encoding == null) { str = CharStreams.fromPath(file_name); } else { str = CharStreams.fromPath(file_name, encoding); } } var lexer = new ArithmeticLexer(str); if (show_tokens) { StringBuilder new_s = new StringBuilder(); for (int i = 0; ; ++i) { var ro_token = lexer.NextToken(); var token = (CommonToken)ro_token; token.TokenIndex = i; new_s.AppendLine(token.ToString()); if (token.Type == Antlr4.Runtime.TokenConstants.EOF) { break; } } System.Console.Error.WriteLine(new_s.ToString()); lexer.Reset(); } var tokens = new CommonTokenStream(lexer); var parser = new ArithmeticParser(tokens); var listener_lexer = new ErrorListener <int>(); var listener_parser = new ErrorListener <IToken>(); lexer.AddErrorListener(listener_lexer); parser.AddErrorListener(listener_parser); DateTime before = DateTime.Now; ArithmeticParser.File_Context tree = parser.file_(); DateTime after = DateTime.Now; System.Console.Error.WriteLine("Time: " + (after - before)); if (listener_lexer.had_error || listener_parser.had_error) { System.Console.Error.WriteLine("Parse failed."); } else { System.Console.Error.WriteLine("Parse succeeded."); } if (show_tree) { System.Console.Error.WriteLine(tree.ToStringTree(parser)); } System.Environment.Exit(listener_lexer.had_error || listener_parser.had_error ? 1 : 0); return(tree); }
/// <summary> /// Init a database. </summary> /// <param name="context"> application context. </param> /// <param name="dbName"> database (file) name. </param> /// <param name="version"> schema version. </param> /// <param name="tableName"> table name. </param> /// <param name="schema"> specimen value. </param> /// <param name="errorListener"> optional error listener. </param> public EngagementStorage(Context context, string dbName, int version, string tableName, ContentValues schema, ErrorListener errorListener) { /* Prepare SQLite manager */ mContext = context; mManager = new SQLiteManager(context, dbName, version, tableName, schema); mErrorListener = errorListener; }
public void setErrorListener(ErrorListener listener) { this.listener = listener; }
static void Main(string[] args) { bool show_tree = false; bool show_tokens = false; string file_name = null; string input = null; for (int i = 0; i < args.Length; ++i) { if (args[i].Equals("-tokens")) { show_tokens = true; continue; } else if (args[i].Equals("-tree")) { show_tree = true; continue; } else if (args[i].Equals("-input")) { input = args[++i]; } else if (args[i].Equals("-file")) { file_name = args[++i]; } } ICharStream str = null; if (input == null && file_name == null) { StringBuilder sb = new StringBuilder(); int ch; while ((ch = System.Console.Read()) != -1) { sb.Append((char)ch); } input = sb.ToString(); str = CharStreams.fromString(input); } else if (input != null) { str = CharStreams.fromString(input); } else if (file_name != null) { str = CharStreams.fromPath(file_name); } var lexer = new ArithmeticLexer(str); if (show_tokens) { StringBuilder new_s = new StringBuilder(); for (int i = 0; ; ++i) { var ro_token = lexer.NextToken(); var token = (CommonToken)ro_token; token.TokenIndex = i; new_s.AppendLine(token.ToString()); if (token.Type == Antlr4.Runtime.TokenConstants.EOF) { break; } } System.Console.Error.WriteLine(new_s.ToString()); lexer.Reset(); } var tokens = new CommonTokenStream(lexer); var parser = new ArithmeticParser(tokens); var listener_lexer = new ErrorListener <int>(); var listener_parser = new ErrorListener <IToken>(); lexer.AddErrorListener(listener_lexer); parser.AddErrorListener(listener_parser); var tree = parser.file(); if (listener_lexer.had_error || listener_parser.had_error) { System.Console.Error.WriteLine("parse failed."); } else { System.Console.Error.WriteLine("parse succeeded."); } if (show_tree) { System.Console.Error.WriteLine(tree.ToStringTree(parser)); } System.Environment.Exit(listener_lexer.had_error || listener_parser.had_error ? 1 : 0); }
public ErrorListenerProxySink(ScriptSource source, ErrorListener listener) { _listener = listener; _source = source; }
public Expression Get(IToken token, Expression[] args, Expression[] contextArgs, ErrorListener errors) { string errorDetails = null; Type[] types = args.Select(arg => arg.Type).ToArray(); try { BuiltinExecutor executor = this.Executors.FirstOrDefault(ex => ex.InputArgs.Match(types, ex.IsQueryValueSupported)); // contextArgs currently are very straightforward. BodyQuery and TwinChangeIncludes use them to get message body. // Not doing a match on internal args to retrieve the executor as of yet if (executor != null) { if (executor.IsQueryValueSupported) { // Wrap all args as QueryValue args = WrapArgsAsQueryValue(args); } return(executor.ExecutorFunc(args, contextArgs)); } } catch (Exception ex) when(!ex.IsFatal()) { errorDetails = ex.Message; } errors.ArgumentError(token, this.Executors.Select(ex => ex.InputArgs).ToArray(), types, errorDetails); return(Expression.Constant(Undefined.Instance)); }
public void RunTool(string ast_file, string spec_file, bool keep_file, string expression, string template_directory, string output_file, bool debug_information) { _ast_file = ast_file; _keep_file = keep_file; _debug_information = debug_information; _expression = expression; _spec_file = spec_file; _template_directory = template_directory; _output_file_name = output_file; Tool.OutputLocation = output_file; if (_output_file_name != null) { var is_file = File.Exists(_output_file_name); var is_directory = Directory.Exists(_output_file_name); if (is_file) { Tool.Redirect = new Redirect(_output_file_name); } } string ast_string = null; if (ast_file == null || ast_file == "") { var s = new List <string>(); string input; while ((input = Console.ReadLine()) != null) { s.Add(input); } ast_string = string.Join("\r\n", s); } else { ast_string = File.ReadAllText(ast_file); } var the_reconstructed_tree = C.Deserialize.ReconstructTree(new CPP14Parser(null), new CPP14Lexer(null), ast_string); var ast_stream = CharStreams.fromstring(ast_string); ITokenSource ast_lexer = new AstLexer(ast_stream); var ast_tokens = new CommonTokenStream(ast_lexer); var ast_parser = new AstParserParser(ast_tokens); ast_parser.BuildParseTree = true; var listener = new ErrorListener <IToken>(); ast_parser.AddErrorListener(listener); IParseTree ast_tree = ast_parser.ast(); if (listener.had_error) { throw new Exception(); } _ast = ast_tree; _common_token_stream = ast_tokens; if (spec_file == null && expression != null) { var exp = new SpecFileAndListener(this); exp.ParseExpressionPattern(expression); var output_engine = new OutputEngine(this); output_engine.Run(true); } else if (spec_file != null) { var full_path = System.IO.Path.GetDirectoryName(System.IO.Path.GetFullPath(typeof(Piggy).Assembly.Location)) + System.IO.Path.DirectorySeparatorChar; var file = new SpecFileAndListener(this); file.ParseSpecFile(_spec_file); var output_engine = new OutputEngine(this); output_engine.Run(false); } }
private static void SetupParser(string objectDecoration, out ObjectDecorationParser parser, out ErrorListener errorListener) { AntlrInputStream inputStream = new AntlrInputStream(objectDecoration); ObjectDecorationLexer lexer = new ObjectDecorationLexer(inputStream); CommonTokenStream commonTokenStream = new CommonTokenStream(lexer); parser = new ObjectDecorationParser(commonTokenStream); parser.RemoveErrorListeners(); errorListener = new ErrorListener(); parser.AddErrorListener(errorListener); }
static void Main(string[] args) { bool show_tree = false; bool show_tokens = false; string file_name = null; string input = null; for (int i = 0; i \ < args.Length; ++i) { if (args[i].Equals("-tokens")) { show_tokens = true; continue; } else if (args[i].Equals("-tree")) { show_tree = true; continue; } else if (args[i].Equals("-input")) { input = args[++i]; } else if (args[i].Equals("-file")) { file_name = args[++i]; } } ICharStream str = null; if (input == null && file_name == null) { str = CharStreams.fromStream(System.Console.OpenStandardInput()); } else if (input != null) { str = CharStreams.fromString(input); } else if (file_name != null) { str = CharStreams.fromPath(file_name); } var lexer = new < lexer_name > (str); if (show_tokens) { StringBuilder new_s = new StringBuilder(); for (int i = 0; ; ++i) { var ro_token = lexer.NextToken(); var token = (CommonToken)ro_token; token.TokenIndex = i; new_s.AppendLine(token.ToString()); if (token.Type == Antlr4.Runtime.TokenConstants.EOF) { break; } } System.Console.Error.WriteLine(new_s.ToString()); lexer.Reset(); } var tokens = new CommonTokenStream(lexer); var parser = new < parser_name > (tokens); var listener_lexer = new ErrorListener \ < int > (); var listener_parser = new ErrorListener \ < IToken > (); lexer.AddErrorListener(listener_lexer); parser.AddErrorListener(listener_parser); DateTime before = DateTime.Now; var tree = parser.< start_symbol > (); DateTime after = DateTime.Now; System.Console.Error.WriteLine("Time: " + (after - before)); if (listener_lexer.had_error || listener_parser.had_error) { System.Console.Error.WriteLine("Parse failed."); } else { System.Console.Error.WriteLine("Parse succeeded."); } if (show_tree) { System.Console.Error.WriteLine(tree.ToStringTree(parser)); } System.Environment.Exit(listener_lexer.had_error || listener_parser.had_error ? 1 : 0); }