public void Print(LNode node, StringBuilder target, IMessageSink sink = null, ParsingMode mode = null, ILNodePrinterOptions options = null) { if (_usePlainCsPrinter) EcsNodePrinter.PrintPlainCSharp(node, target, sink, mode, options); else EcsNodePrinter.PrintECSharp(node, target, sink, mode, options); }
public IListSource<LNode> Parse(ILexer<Token> input, IMessageSink msgs, ParsingMode inputType = null, bool preserveComments = true) { var preprocessed = new EcsPreprocessor(input, preserveComments); var treeified = new TokensToTree(preprocessed, false); var results = Parse(treeified.Buffered(), input.SourceFile, msgs, inputType); if (preserveComments) { var injector = new EcsTriviaInjector(preprocessed.TriviaList, input.SourceFile, (int)TokenType.Newline, "/*", "*/", "//"); return injector.Run(results.GetEnumerator()).Buffered(); } else return results; }
/// <summary>Converts a sequences of LNodes to strings, adding a line separator between each.</summary> /// <param name="printer">Printer to be used for each single LNode.</param> /// <remarks>The newline between two nodes is suppressed if the second /// node has a <c>#trivia_appendStatement</c> attribute.</remarks> public static StringBuilder PrintMultiple(ILNodePrinter printer, IEnumerable<LNode> nodes, StringBuilder sb, IMessageSink sink, ParsingMode mode, ILNodePrinterOptions options) { sb = sb ?? new StringBuilder(); var lineSeparator = (options != null ? options.NewlineString : null) ?? "\n"; bool first = true; foreach (LNode node in nodes) { if (!first) sb.Append(node.AttrNamed(CodeSymbols.TriviaAppendStatement) == null ? lineSeparator : " "); printer.Print(node, sb, sink, mode, options); first = false; } return sb; }
/// <summary>Parses a string by invoking <see cref="IParsingService.Parse(ICharSource, string, IMessageSink, ParsingMode, bool)"/> using an empty string as the file name.</summary> public static IListSource<LNode> Parse(this IParsingService parser, UString input, IMessageSink msgs = null, ParsingMode inputType = null, bool preserveComments = true) { return parser.Parse(input, "", msgs ?? MessageSink.Default, inputType, preserveComments); }
/// <summary>Serializes a list of syntax trees to a string in the /// syntax supported by the specified <see cref="ILNodePrinter"/>.</summary> public static string Print(this ILNodePrinter printer, IEnumerable<LNode> nodes, IMessageSink sink = null, ParsingMode mode = null, ILNodePrinterOptions options = null) { StringBuilder target = new StringBuilder(); printer.Print(nodes, target, sink, mode, options); return target.ToString(); }
public IListSource<LNode> Parse(IListSource<Token> input, ISourceFile file, IMessageSink msgs, ParsingMode inputType = null) { // For efficiency we'd prefer to re-use our _parser object, but // when parsing lazily, we can't re-use it because another parsing // operation could start before this one is finished. To force // greedy parsing, we can call ParseStmtsGreedy(), but the caller may // prefer lazy parsing, especially if the input is large. As a // compromise I'll check if the source file is larger than a // certain arbitrary size. Also, ParseExprs() is always greedy // so we can always re-use _parser in that case. char _ = '\0'; if (file.Text.TryGet(255, ref _) || inputType == ParsingMode.FormalArguments || inputType == ParsingMode.Types || inputType == ParsingMode.Expressions) { EcsParser parser = _parser; if (parser == null) _parser = parser = new EcsParser(input, file, msgs); else { parser.ErrorSink = msgs ?? MessageSink.Default; parser.Reset(input, file); } if (inputType == ParsingMode.Expressions) return parser.ParseExprs(false, allowUnassignedVarDecl: false); else if (inputType == ParsingMode.FormalArguments) return parser.ParseExprs(false, allowUnassignedVarDecl: true); else if (inputType == ParsingMode.Types) return LNode.List(parser.DataType()); else return parser.ParseStmtsGreedy(); } else { var parser = new EcsParser(input, file, msgs); return parser.ParseStmtsLazy().Buffered(); } }
public static IListSource <LNode> Parse(this IParsingService parser, ILexer <Token> input, IMessageSink msgs = null, ParsingMode mode = null, bool preserveComments = true) { return(parser.Parse(input, msgs, QuickOptions(mode, preserveComments))); }
/// <summary>Parses a Stream.</summary> public static IListSource<LNode> Parse(this IParsingService parser, Stream stream, string fileName, ParsingMode inputType = null, IMessageSink msgs = null, bool preserveComments = true) { return parser.Parse(new StreamCharSource(stream), fileName, msgs, inputType, preserveComments); }
/// <summary>Parses a Stream.</summary> public static IListSource <LNode> Parse(this IParsingService parser, Stream stream, string fileName, ParsingMode inputType = null, IMessageSink msgs = null, bool preserveComments = true) { return(parser.Parse(new StreamCharSource(stream), fileName, msgs, inputType, preserveComments)); }
/// <summary>Opens the specified file, parses the entire file, and closes the file.</summary> public static IListSource <LNode> ParseFile(this IParsingService parser, string fileName, IMessageSink msgs = null, ParsingMode inputType = null, bool preserveComments = true) { using (var stream = new FileStream(fileName, FileMode.Open)) { var results = Parse(parser, stream, fileName, inputType ?? ParsingMode.File, msgs, preserveComments); // TODO: think about whether we should explicitly document or spec this out... // If we're not careful, the caller gets a "Cannot access a closed file" // exception. The problem is that IParsingService.Parse() may parse the // file lazily, so we can't close the file (as `using` does for us) until // we make sure it is fully parsed. Luckily this is easy: just invoke the // Count property, which can only be computed by parsing the whole file. var _ = results.Count; return(results); } }
/// <inheritdoc cref="ParseSingle(IParsingService, ICharSource, string, IMessageSink, IParsingOptions)"/> public static LNode ParseSingle(this IParsingService parser, ICharSource text, string fileName, IMessageSink msgs = null, ParsingMode inputType = null, bool preserveComments = true) { return(ParseSingle(parser, text, fileName, msgs, QuickOptions(inputType, preserveComments))); }
/// <inheritdoc cref="ParseSingle(IParsingService, UString, IMessageSink, IParsingOptions)"/> public static LNode ParseSingle(this IParsingService parser, UString expr, IMessageSink msgs = null, ParsingMode inputType = null, bool preserveComments = true) { return(ParseSingle(parser, expr, msgs, QuickOptions(inputType, preserveComments))); }
public static IListSource <LNode> Parse(this IParsingService parser, IListSource <Token> tokens, ISourceFile file, IMessageSink msgs, ParsingMode inputType = null) { return(parser.Parse(tokens, file, msgs, QuickOptions(inputType))); }
/// <summary>Parses a string and expects exactly one output.</summary> /// <exception cref="InvalidOperationException">The output list was empty or contained multiple nodes.</exception> public static LNode ParseSingle(this IParsingService parser, UString expr, IMessageSink msgs = null, ParsingMode inputType = null, bool preserveComments = true) { var e = parser.Parse(expr, msgs, inputType, preserveComments); return Single(e); }
public void Print(IEnumerable<LNode> nodes, StringBuilder target, IMessageSink sink = null, ParsingMode mode = null, ILNodePrinterOptions options = null) { LNodePrinter.PrintMultiple(this, nodes, target, sink, mode, options); }
/// <summary>Parses a string and expects exactly one output.</summary> /// <exception cref="InvalidOperationException">The output list was empty or contained multiple nodes.</exception> public static LNode ParseSingle(this IParsingService parser, ICharSource text, string fileName, IMessageSink msgs = null, ParsingMode inputType = null, bool preserveComments = true) { var e = parser.Parse(text, fileName, msgs, inputType, preserveComments); return Single(e); }
public IListSource<LNode> Parse(ICharSource text, string fileName, IMessageSink msgs, ParsingMode inputType = null, bool preserveComments = true) { var lexer = Tokenize(text, fileName, msgs); return Parse(lexer, msgs, inputType, preserveComments); }
/// <summary>Opens the specified file and parses it.</summary> public static IListSource<LNode> ParseFile(this IParsingService parser, string fileName, IMessageSink msgs = null, ParsingMode inputType = null, bool preserveComments = true) { using (var stream = new FileStream(fileName, FileMode.Open)) return Parse(parser, stream, fileName, inputType ?? ParsingMode.File, msgs, preserveComments); }
public static IListSource <LNode> Parse(this IParsingService parser, ICharSource text, string fileName, IMessageSink msgs = null, ParsingMode inputType = null, bool preserveComments = true) { return(parser.Parse(text, fileName, msgs ?? MessageSink.Default, QuickOptions(inputType, preserveComments))); }