internal virtual void RunParseTest(string document, Func <ParserBase, Action> parserActionSelector, Block expectedRoot, IList <RazorError> expectedErrors, bool designTimeParser) { // Create the source using (StringTextBuffer reader = new StringTextBuffer(document)) { ParserResults results = null; try { ParserBase codeParser = CreateCodeParser(); MarkupParser markupParser = CreateMarkupParser(); SyntaxTreeBuilderVisitor listener = new SyntaxTreeBuilderVisitor(); ParserContext context = CreateParserRun(new TextBufferReader(reader), codeParser, markupParser, listener); context.DesignTimeMode = designTimeParser; codeParser.Context = context; markupParser.Context = context; // Run the parser parserActionSelector(context.ActiveParser)(); context.OnComplete(); // Collect the results results = listener.Results; EvaluateResults(TestContext, results, expectedRoot, expectedErrors); } finally { if (TestContext != null && results != null && results.Document != null) { TestContext.WriteLine(String.Empty); TestContext.WriteLine("Actual Parse Tree:"); WriteNode(0, TestContext, results.Document); } } } }
/// <summary> /// Initialises a new instance of <see cref="CSharpDirectCompilerService"/>. /// </summary> /// <param name="strictMode">Specifies whether the strict mode parsing is enabled.</param> /// <param name="markupParser">The markup parser to use.</param> public CSharpDirectCompilerService(bool strictMode = true, MarkupParser markupParser = null) : base( new CSharpRazorCodeLanguage(strictMode), new CSharpCodeProvider(), markupParser) { }
/// <summary> /// Initialises a new instance of <see cref="RazorCompiler"/> /// </summary> /// <param name="provider">The language provider used to create language services.</param> /// <param name="baseType">[Optional] The template base type.</param> /// <param name="parser">[Optional] The markup parser.</param> public RazorCompiler(ILanguageProvider provider, Type baseType = null, MarkupParser parser = null) { if (provider == null) { throw new ArgumentNullException("provider"); } if (baseType != null && !typeof(ITemplate).IsAssignableFrom(baseType)) { throw new ArgumentException( string.Format("{0} is not a valid template base. Templates must inherit from ITemplate.", baseType.FullName)); } // Need to initialise this type to ensure assemblies are loaded for referencing. Eugh.... var temp1 = Microsoft.CSharp.RuntimeBinder.CSharpBinderFlags.None; languageProvider = provider; templateBaseType = baseType; markupParser = parser ?? new HtmlMarkupParser(); Namespaces = new List <string> { "System", "System.Collections.Generic", "System.Linq" }; }
/// <summary> /// Returns a Phrase that has been constructed taking in account /// the value of some attributes. /// </summary> /// <param name="attributes">some attributes</param> public Phrase(Properties attributes) : this("", FontFactory.getFont(attributes)) { this.Clear(); string value; if ((value = attributes.Remove(ElementTags.LEADING)) != null) { Leading = float.Parse(value); } else if ((value = attributes.Remove(MarkupTags.CSS_LINEHEIGHT)) != null) { Leading = MarkupParser.parseLength(value); } if ((value = attributes.Remove(ElementTags.ITEXT)) != null) { Chunk chunk = new Chunk(value); if ((value = attributes.Remove(ElementTags.GENERICTAG)) != null) { chunk.setGenericTag(value); } Add(chunk); } if (attributes.Count > 0) { MarkupAttributes = attributes; } }
/// <summary> /// Returns a Phrase that has been constructed taking in account /// the value of some attributes. /// </summary> /// <param name="attributes">some attributes</param> public Phrase(Properties attributes) : this("", FontFactory.GetFont(attributes)) { this.Clear(); string value; if ((value = attributes.Remove(ElementTags.LEADING)) != null) { Leading = float.Parse(value, System.Globalization.NumberFormatInfo.InvariantInfo); } else if ((value = attributes.Remove(MarkupTags.CSS_KEY_LINEHEIGHT)) != null) { Leading = MarkupParser.ParseLength(value); } if ((value = attributes.Remove(ElementTags.ITEXT)) != null) { Chunk chunk = new Chunk(value); if ((value = attributes.Remove(ElementTags.GENERICTAG)) != null) { chunk.SetGenericTag(value); } Add(chunk); } if (attributes.Count > 0) { MarkupAttributes = attributes; } }
/// <summary> /// Returns a ListItem that has been constructed taking in account /// the value of some attributes. /// </summary> /// <param name="attributes">Some attributes</param> public ListItem(Properties attributes) : base("", FontFactory.getFont(attributes)) { string value; if ((value = attributes.Remove(ElementTags.ITEXT)) != null) { Add(new Chunk(value)); } if ((value = attributes.Remove(ElementTags.LEADING)) != null) { this.Leading = float.Parse(value); } else if ((value = attributes.Remove(MarkupTags.CSS_LINEHEIGHT)) != null) { this.Leading = MarkupParser.parseLength(value); } if ((value = attributes.Remove(ElementTags.INDENTATIONLEFT)) != null) { this.IndentationLeft = float.Parse(value); } if ((value = attributes.Remove(ElementTags.INDENTATIONRIGHT)) != null) { this.IndentationRight = float.Parse(value); } if ((value = attributes.Remove(ElementTags.ALIGN)) != null) { setAlignment(value); } if (attributes.Count > 0) { this.MarkupAttributes = attributes; } }
private void SetTextFromMarkupText(string markupText) { MarkupParser markupParser = new MarkupParser(); markupParser.Markup = markupText; markupParser.ReplaceAnchorValues(this.DataSource, this.ListSeparator); StringBuilder stringBuilder = new StringBuilder(); base.Links.Clear(); foreach (object obj in markupParser.Nodes) { XmlNode xmlNode = (XmlNode)obj; if (XmlNodeType.Element == xmlNode.NodeType && "a" == xmlNode.Name) { XmlAttribute xmlAttribute = xmlNode.Attributes["id"]; if (xmlAttribute != null) { base.Links.Add(new StringInfo(stringBuilder.ToString()).LengthInTextElements, new StringInfo(xmlNode.InnerText).LengthInTextElements, xmlAttribute.Value); } } stringBuilder.Append(xmlNode.InnerText); } if (base.Text == stringBuilder.ToString() && !string.IsNullOrEmpty(base.Text)) { this.OnTextChanged(EventArgs.Empty); } base.Text = stringBuilder.ToString(); }
/// <summary> /// Gets an instance of the markup parser and is provided an opportunity to decorate or replace it /// </summary> /// <param name="incomingMarkupParser">The markup parser</param> /// <returns>Either the same markup parser, after modifications, or a different markup parser</returns> public virtual MarkupParser DecorateMarkupParser(MarkupParser incomingMarkupParser) { if (incomingMarkupParser == null) { throw new ArgumentNullException("incomingMarkupParser"); } return(incomingMarkupParser); }
public void ParseTypeAttributeTest() { //Parse tokens MarkupParser markupParser = new MarkupParser(Init("type1")); TypeAttribute parsedTypeAttribute = markupParser.ParseTypeAttribute(); //Check Id Attribute Assert.AreEqual("type1", parsedTypeAttribute.GetType()); }
public void ParseNameAttributeTest() { //Parse tokens MarkupParser markupParser = new MarkupParser(Init("nametest")); NameAttribute parsedNameAttribute = markupParser.ParseNameAttribute(); //Check Id Attribute Assert.AreEqual("nametest", parsedNameAttribute.GetName()); }
public void ParseClassAttributeTest() { //Parse tokens MarkupParser markupParser = new MarkupParser(Init("classname")); ClassAttribute parsedClassAttribute = markupParser.ParseClassAttribute(); //Check Id Attribute Assert.AreEqual("classname", parsedClassAttribute.GetClass()); }
public void ParseIdAttributeTest() { //Parse tokens MarkupParser markupParser = new MarkupParser(Init("testid")); IdAttribute parsedIdAttribute = markupParser.ParseIdAttribute(); //Check Id Attribute Assert.AreEqual("testid", parsedIdAttribute.GetId()); }
private ParserResults ParseCore(ITextDocument input) { // Setup the parser context var context = new ParserContext(input, CodeParser, MarkupParser, MarkupParser) { DesignTimeMode = DesignTimeMode }; MarkupParser.Context = context; CodeParser.Context = context; // Execute the parse MarkupParser.ParseDocument(); // Get the result var results = context.CompleteParse(); // Rewrite whitespace if supported var rewritingContext = new RewritingContext(results.Document); foreach (ISyntaxTreeRewriter rewriter in Optimizers) { rewriter.Rewrite(rewritingContext); } if (TagHelperDescriptorResolver != null) { var descriptors = GetTagHelperDescriptors(rewritingContext.SyntaxTree); var tagHelperProvider = new TagHelperDescriptorProvider(descriptors); var tagHelperParseTreeRewriter = new TagHelperParseTreeRewriter(tagHelperProvider); // Rewrite the document to utilize tag helpers tagHelperParseTreeRewriter.Rewrite(rewritingContext); } var syntaxTree = rewritingContext.SyntaxTree; // Link the leaf nodes into a chain Span prev = null; foreach (Span node in syntaxTree.Flatten()) { node.Previous = prev; if (prev != null) { prev.Next = node; } prev = node; } // We want to surface both the parsing and rewriting errors as one unified list of errors because // both parsing and rewriting errors affect the end users Razor page. var errors = results.ParserErrors.Concat(rewritingContext.Errors).ToList(); // Return the new result return(new ParserResults(syntaxTree, errors)); }
public void ParseDefaultMarkupTest() { //Parse tokens MarkupParser markupParser = new MarkupParser(Init("(\"test\")")); Markup parsedMarkup = markupParser.ParseMarkup(); //Check attributes Assert.AreEqual(0, parsedMarkup.GetArguments().Count); }
public void ParseWidthAttributeTest() { //Parse tokens MarkupParser markupParser = new MarkupParser(Init("120")); WidthAttribute parsedWidthAttribute = markupParser.ParseWidthAttribute(); //Check Id Attribute Assert.AreEqual(120, parsedWidthAttribute.GetWidth()); }
public ParseStateMachine([NotNull] MarkupParser parser) { Guard.NotNull(parser, nameof(parser)); this.parser = parser; offset = -1; spanStartIndex = -1; spanEndIndex = -1; }
private ParserResults ParseCore(ITextDocument input) { // Setup the parser context var errorSink = new ErrorSink(); var context = new ParserContext(input, CodeParser, MarkupParser, MarkupParser, errorSink) { DesignTimeMode = DesignTimeMode }; MarkupParser.Context = context; CodeParser.Context = context; // Execute the parse MarkupParser.ParseDocument(); // Get the result var results = context.CompleteParse(); // Rewrite whitespace if supported var rewritingContext = new RewritingContext(results.Document, errorSink); foreach (ISyntaxTreeRewriter rewriter in Optimizers) { rewriter.Rewrite(rewritingContext); } var descriptors = Enumerable.Empty <TagHelperDescriptor>(); if (TagHelperDescriptorResolver != null) { descriptors = GetTagHelperDescriptors(rewritingContext.SyntaxTree, rewritingContext.ErrorSink); var tagHelperProvider = new TagHelperDescriptorProvider(descriptors); var tagHelperParseTreeRewriter = new TagHelperParseTreeRewriter(tagHelperProvider); // Rewrite the document to utilize tag helpers tagHelperParseTreeRewriter.Rewrite(rewritingContext); } var syntaxTree = rewritingContext.SyntaxTree; // Link the leaf nodes into a chain Span prev = null; foreach (Span node in syntaxTree.Flatten()) { node.Previous = prev; if (prev != null) { prev.Next = node; } prev = node; } // Return the new result return(new ParserResults(syntaxTree, descriptors, errorSink)); }
/// <summary> /// Initialises a new instance of <see cref="CompilerServiceBase"/> /// </summary> /// <param name="codeLanguage">The code language.</param> /// <param name="markupParser">The markup parser.</param> protected CompilerServiceBase(RazorCodeLanguage codeLanguage, MarkupParser markupParser) { if (codeLanguage == null) { throw new ArgumentNullException("codeLanguage"); } CodeLanguage = codeLanguage; MarkupParser = markupParser ?? new HtmlMarkupParser(); }
public void ParseCallNoArgsMarkupTest() { //Parse tokens MarkupParser markupParser = new MarkupParser(Init("home()")); Markup parsedMarkup = markupParser.ParseMarkup(); //Check markup Assert.AreEqual("home", parsedMarkup.GetDesignator().GetIdentifier()); //identifier check Assert.AreEqual(0, parsedMarkup.GetDesignator().GetAttributes().Count); //no attributes Assert.AreEqual(0, parsedMarkup.GetArguments().Count); //no arguments }
public void ParseNoArgumentsTest() { //Parse tokens MarkupParser markupParser = new MarkupParser(Init("()")); Markup markup = new Markup(); markupParser.ParseArguments(markup); //Test arguments Assert.AreEqual(0, markup.GetArguments().Count); }
public Font GetFont(ChainedProperties props) { String face = props["face"]; if (face != null) { StringTokenizer tok = new StringTokenizer(face, ","); while (tok.HasMoreTokens()) { face = tok.NextToken().Trim(); if (face.StartsWith("\"")) { face = face.Substring(1); } if (face.EndsWith("\"")) { face = face.Substring(0, face.Length - 1); } if (fontImp.IsRegistered(face)) { break; } } } int style = 0; if (props.HasProperty("i")) { style |= Font.ITALIC; } if (props.HasProperty("b")) { style |= Font.BOLD; } if (props.HasProperty("u")) { style |= Font.UNDERLINE; } String value = props["size"]; float size = 12; if (value != null) { size = float.Parse(value, System.Globalization.NumberFormatInfo.InvariantInfo); } Color color = MarkupParser.DecodeColor(props["color"]); String encoding = props["encoding"]; if (encoding == null) { encoding = BaseFont.WINANSI; } return(fontImp.GetFont(face, encoding, true, size, style, color)); }
public void DecorateMarkupParserReturnsIncomingParser() { // Arrange MarkupParser expected = new HtmlMarkupParser(); // Act MarkupParser actual = CreateHost().DecorateMarkupParser(expected); // Assert Assert.AreSame(expected, actual); }
public void ParseAttributeTest() { //Parse tokens MarkupParser markupParser = new MarkupParser(Init("#id1")); Attribute parsedAttribute = markupParser.ParseAttribute(); //Check attribute Assert.AreEqual(typeof(IdAttribute), parsedAttribute.GetType()); //Check id attribute Assert.AreEqual("#id1", parsedAttribute.ToString()); }
/// <summary> /// Returns a Chunk that has been constructed taking in account /// the value of some attributes. /// </summary> /// <param name="attributes">some attributes</param> public Chunk(Properties attributes) : this("", FontFactory.GetFont(attributes)) { string value; if ((value = attributes.Remove(ElementTags.ITEXT)) != null) { Append(value); } if ((value = attributes.Remove(ElementTags.LOCALGOTO)) != null) { SetLocalGoto(value); } if ((value = attributes.Remove(ElementTags.REMOTEGOTO)) != null) { String destination = attributes.Remove(ElementTags.DESTINATION); String page = attributes.Remove(ElementTags.PAGE); if (page != null) { SetRemoteGoto(value, int.Parse(page)); } else if (destination != null) { SetRemoteGoto(value, destination); } } if ((value = attributes.Remove(ElementTags.LOCALDESTINATION)) != null) { SetLocalDestination(value); } if ((value = attributes.Remove(ElementTags.SUBSUPSCRIPT)) != null) { SetTextRise(float.Parse(value, System.Globalization.NumberFormatInfo.InvariantInfo)); } if ((value = attributes.Remove(MarkupTags.CSS_KEY_VERTICALALIGN)) != null && value.EndsWith("%")) { float p = float.Parse(value.Substring(0, value.Length - 1), System.Globalization.NumberFormatInfo.InvariantInfo) / 100f; SetTextRise(p * font.Size); } if ((value = attributes.Remove(ElementTags.GENERICTAG)) != null) { SetGenericTag(value); } if ((value = attributes.Remove(ElementTags.BACKGROUNDCOLOR)) != null) { SetBackground(MarkupParser.DecodeColor(value)); } if (attributes.Count > 0) { this.MarkupAttributes = attributes; } }
/// <remarks> This function will update Result as a side-effect </remarks> /// <returns> (RebuildRequired | (Started (LogEvent)* [BytecodeGenerated] Ended))* </returns> public IObservable <IBinaryMessage> Reify(IObservable <GenerateBytecode> args) { return(_builder.Result.Switch(maybeBuild => maybeBuild.MatchWith( none: () => { // Can't do any reifying without a build _result.OnNext(Optional.None()); return args.Select(arg => (IBinaryMessage) new RebuildRequired()); }, some: build => { var markupParser = new MarkupParser( build, new UxParser( build.Project, new GhostCompilerFactory(build.TypeInfo))); // Let's answer those reify calls with some proper bytecode and build events return args .Select(a => Observable.Create <IBinaryMessage>(observer => { observer.OnNext(new Started { Command = a }); var markup = markupParser.TryParseDocuments(a, observer, a.Id); _result.OnNext(markup); var bytecode = TryCompile(markup, observer, a.Id); if (bytecode.HasValue) { observer.OnNext(new BytecodeGenerated(bytecode.Value)); } observer.OnNext(new Ended { Command = a, Success = bytecode.HasValue, BuildDirectory = AbsoluteFilePath.Parse(build.Assembly).ContainingDirectory }); observer.OnCompleted(); return Disposable.Empty; })) .Concat(); }))); }
/// <summary> /// Creates an instance of a template service from configuration. /// </summary> /// <param name="config">The configuration of the template service.</param> /// <returns>An instance of <see cref="TemplateService"/>.</returns> public static TemplateService CreateTemplateService(TemplateServiceConfigurationElement config) { if (config == null) { throw new ArgumentNullException("config"); } MarkupParser parser = null; if (!string.IsNullOrWhiteSpace(config.MarkupParser)) { parser = CreateMarkupParser(config.MarkupParser); } return(CreateTemplateService(config.Language, config.StrictMode, parser)); }
/// <summary> /// Returns a Paragraph that has been constructed taking in account /// the value of some attributes. /// </summary> /// <param name="attributes">Some attributes</param> public Paragraph(Properties attributes) : this("", FontFactory.GetFont(attributes)) { string value; if ((value = attributes.Remove(ElementTags.ITEXT)) != null) { Chunk chunk = new Chunk(value); if ((value = attributes.Remove(ElementTags.GENERICTAG)) != null) { chunk.SetGenericTag(value); } Add(chunk); } if ((value = attributes.Remove(ElementTags.ALIGN)) != null) { SetAlignment(value); } if ((value = attributes.Remove(ElementTags.LEADING)) != null) { this.Leading = float.Parse(value, System.Globalization.NumberFormatInfo.InvariantInfo); } else if ((value = attributes.Remove(MarkupTags.CSS_KEY_LINEHEIGHT)) != null) { this.Leading = MarkupParser.ParseLength(value); } else { this.Leading = 16; } if ((value = attributes.Remove(ElementTags.INDENTATIONLEFT)) != null) { this.IndentationLeft = float.Parse(value, System.Globalization.NumberFormatInfo.InvariantInfo); } if ((value = attributes.Remove(ElementTags.INDENTATIONRIGHT)) != null) { IndentationRight = float.Parse(value, System.Globalization.NumberFormatInfo.InvariantInfo); } if ((value = attributes.Remove(ElementTags.KEEPTOGETHER)) != null) { keeptogether = bool.Parse(value); } if (attributes.Count > 0) { MarkupAttributes = attributes; } }
/// <summary> /// Creates an instance of a <see cref="TemplateService"/>. /// </summary> /// <param name="configuration">The <see cref="TemplateServiceConfigurationElement"/> that represents the configuration.</param> /// <param name="defaultNamespaces">The enumerable of namespaces to add as default.</param> /// <returns>A new instance of <see cref="TemplateService"/>.</returns> public static TemplateService CreateTemplateService(TemplateServiceConfigurationElement configuration, IEnumerable <string> defaultNamespaces = null) { if (configuration == null) { throw new ArgumentNullException("configuration"); } ILanguageProvider provider = null; MarkupParser parser = null; Type templateBaseType = null; if (!string.IsNullOrEmpty(configuration.LanguageProvider)) { provider = (ILanguageProvider)GetInstance(configuration.LanguageProvider); } if (!string.IsNullOrEmpty(configuration.MarkupParser)) { parser = (MarkupParser)GetInstance(configuration.MarkupParser); } if (!string.IsNullOrEmpty(configuration.TemplateBase)) { templateBaseType = GetType(configuration.TemplateBase); } var namespaces = configuration.Namespaces .Cast <NamespaceConfigurationElement>() .Select(n => n.Namespace); if (defaultNamespaces != null) { namespaces = defaultNamespaces .Concat(namespaces) .Distinct(); } var service = new TemplateService(provider, templateBaseType, parser); foreach (string ns in namespaces) { service.Namespaces.Add(ns); } return(service); }
/// <summary> /// Create an instance of <see cref="MarkupParser"/>. /// </summary> /// <param name="typeName">The <see cref="MarkupParser"/> type.</param> /// <returns>An instance of <see cref="MarkupParser"/>.</returns> private static MarkupParser CreateMarkupParser(string typeName) { Type parserType = Type.GetType(typeName); if (parserType == null) { throw new ConfigurationErrorsException("The parser type '" + typeName + "' could not be loaded."); } MarkupParser parser = Activator.CreateInstance(parserType) as MarkupParser; if (parser == null) { throw new ConfigurationErrorsException("The type '" + typeName + "' is not a markup parser."); } return(parser); }
public DocumentWithSpans GetDocumentWithSpansFromMarkup([NotNull] AnalyzerTestContext context) { Guard.NotNull(context, nameof(context)); var parser = new MarkupParser(context.MarkupCode); CodeWithSpans codeWithSpans = parser.Parse(); ParseOptions parseOptions = GetParseOptions(context.DocumentationMode, context.LanguageName); CompilationOptions compilationOptions = GetCompilationOptions(context.CompilerWarningLevel, context.LanguageName); Document document = new AdhocWorkspace() .AddProject(context.AssemblyName, context.LanguageName) .WithParseOptions(parseOptions) .WithCompilationOptions(compilationOptions) .AddMetadataReferences(context.References) .AddDocument(context.FileName, codeWithSpans.Code); return(new DocumentWithSpans(document, codeWithSpans.Spans)); }