/// <summary> /// Interpret CommentStatement /// </summary> /// <param name="statement">CommentStatement to interpret</param> public override void Visit(CommentStatement statement) { XHTMLElement comment = new XHTMLElement("comment", Current); comment.SetTagState(false); comment.AddContent(statement.GetCommentString()); AddElement(comment); }
public BlockStatement ParseBlockStatement(BlockSyntax syntax, SemanticModel semanticModel) { // ライン取得 var blockLineSpan = syntax.SyntaxTree.GetLineSpan(syntax.Span); List <Statement> statements = new List <Statement>(); List <FileLinePositionSpan> statementSpans = new List <FileLinePositionSpan>(); // 中身 foreach (var statement in syntax.Statements) { var statementLineSpan = statement.SyntaxTree.GetLineSpan(statement.Span); var result = ParseStatement(statement, semanticModel); if (result != null) { result.StartingLine = statementLineSpan.StartLinePosition.Line - blockLineSpan.StartLinePosition.Line; result.EndingLine = statementLineSpan.EndLinePosition.Line - blockLineSpan.StartLinePosition.Line; statementSpans.Add(statementLineSpan); statements.Add(result); } } // コメント foreach (var t in syntax.DescendantTrivia()) { if (t.IsKind(SyntaxKind.SingleLineCommentTrivia)) { var commentLineSpan = t.SyntaxTree.GetLineSpan(t.Span); // ステートメント内は含まない if (statementSpans.Any(_ => _.StartLinePosition.Line <= commentLineSpan.StartLinePosition.Line && commentLineSpan.EndLinePosition.Line <= _.EndLinePosition.Line)) { continue; } var result = new CommentStatement(); result.Text = t.ToString(); result.Text = result.Text.Substring(2).Trim(); result.StartingLine = commentLineSpan.StartLinePosition.Line - blockLineSpan.StartLinePosition.Line; result.EndingLine = commentLineSpan.EndLinePosition.Line - blockLineSpan.StartLinePosition.Line; statements.Add(result); } } statements = statements.OrderBy(_ => _.StartingLine).ToList(); var bs = new BlockStatement(); bs.Statements = statements.ToArray(); return(bs); }
public void CSharpCodeGenerator_CommentNull() { var statement = new CommentStatement(); var generator = new CSharpCodeGenerator(); var result = generator.Write(statement); Assert.That.StringEquals(@"// ", result); }
public void CommentStatementProducesCommentExplicitlyWithPrefixes() { CommentStatement comment = new CommentStatement("Comment") { AddExtraLineBreaks = true, Style = CommentStyle.OneLineComments }; Assert.AreEqual("\r\n// Comment\r\n", comment.ToString()); }
public void CSharpCodeGenerator_CommentMultiLine() { var statement = new CommentStatement("test1" + Environment.NewLine + Environment.NewLine + "test2"); var generator = new CSharpCodeGenerator(); var result = generator.Write(statement); Assert.That.StringEquals(@"// test1 // // test2 ", result); }
/// <summary> /// Parser for CommentStatement /// </summary> /// <returns>Parsed CommentStatement</returns> public CommentStatement ParseCommentStatement() { CommentStatement commentStatement = new CommentStatement(); //Skip comment token NextToken("comment", "comment thisisacomment;", "comment"); //Parse comment CurrentToken = TokenStream.NextToken(); commentStatement.SetCommentString(CurrentToken.GetValue().ToString()); //Skip ; token NextToken(";", "comment thisisacomment;", ';'); return(commentStatement); }
public void CommentStatementProducesCommentWithPrefixes() { CommentStatement comment = new CommentStatement("Comment\r\nAndThenSome"); Assert.AreEqual("// Comment\r\n// AndThenSome\r\n", comment.ToString()); }
public void CommentStatementProducesComment() { CommentStatement comment = new CommentStatement("Comment"); Assert.AreEqual("/* Comment */", comment.ToString()); }
public void VisitComment(CommentStatement c) { gen.Comment(c.comment); }
public void VisitComment(CommentStatement c) { gen.Comment(c.Comment !); }
public void VisitComment(CommentStatement c) { w.Write("#{0}", c.comment); }
private Statement GetStatement(bool pardonInvalid = true) { Statement ret = null; var loc = Current.Begin; SkipWhitespaces(); switch (Current.Kind) { case LexemeKind.EndOfFile: return(null); case LexemeKind.Hashtag: AdvanceUntil(LexemeKind.NewLine); ret = new CommentStatement(); break; case LexemeKind.Keyword when Current.Content == "return": Advance(); SkipWhitespaces(false); ret = new ReturnStatement(Current.Kind == LexemeKind.NewLine ? null : DoExpression()); break; case LexemeKind.Keyword when Current.Content == "function": ret = DoFunction(); break; case LexemeKind.Keyword when Current.Content == "if": ret = DoIfStatement(); break; case LexemeKind.Keyword when Current.Content == "while": ret = DoWhileStatement(); break; case LexemeKind.Keyword when Current.Content == "for": ret = DoForStatement(); break; case LexemeKind.Keyword: Error("unexpected keyword: " + Current.Content); break; case LexemeKind.String: SkipWhitespaces(); if (Current.Kind != LexemeKind.String && Current.Kind != LexemeKind.QuotedString) { return(null); } ret = Peek(LexemeKind.Exclamation) ? new ExpressionStatement(DoFunctionCall()) : new ExpressionStatement(DoCommandExpression()); break; case LexemeKind.Dollar: Advance(); ret = new ExpressionStatement(DoVariableAssign()); break; case LexemeKind.AtSign: ret = DoDirectiveStatement(); break; } if (ret != null) { ret.Location = loc; } else if (!pardonInvalid) { Error($"unexpected {Current.Kind} found"); } return(ret); }
public bool TryParse(string template, out IList <Statement> result, out IEnumerable <string> errors) { errors = Array.Empty <string>(); var segment = new StringSegment(template); Parser parser = null; _context = new ParserContext(); result = _context.CurrentBlock.Statements; try { bool trimBefore = false; bool trimAfter = false; int previous = 0, index = 0; Statement s; while (true) { previous = index; if (!MatchTag(segment, index, out var start, out var end)) { index = segment.Length; if (index != previous) { // Consume last Text statement ConsumeTextStatement(segment, previous, index, trimAfter, false); } break; } else { trimBefore = segment.Buffer[start + 2] == '-'; // Only create a parser if there are tags in the template if (parser == null) { parser = new Parser(_languageData); } if (start != previous) { // Consume current Text statement ConsumeTextStatement(segment, previous, start, trimAfter, trimBefore); } trimAfter = segment.Buffer[end - 2] == '-'; var tag = segment.Substring(start, end - start + 1); if (trimAfter || trimBefore) { // Remove the dashes for the parser StringBuilder sb = new StringBuilder(tag); if (trimBefore) { sb[2] = ' '; } if (trimAfter) { sb[end - start - 2] = ' '; } tag = sb.ToString(); } var tree = parser.Parse(tag); if (tree.HasErrors()) { int line = 1, col = 1; foreach (var ch in segment.Buffer.Take(start)) { switch (ch) { case '\n': line++; col = 1; break; case '\r': // Ignore break; default: col++; break; } } errors = tree .ParserMessages .Select(x => $"{x.Message} at line:{line + x.Location.Line}, col:{col + x.Location.Column}") .ToArray(); return(false); } switch (tree.Root.Term.Name) { case "output": s = BuildOutputStatement(tree.Root); break; case "tag": s = BuildTagStatement(tree.Root); break; default: s = null; break; } index = end + 1; // Entered a comment block? if (_isComment) { s = new CommentStatement(ConsumeTag(segment, end + 1, "endcomment", out end)); index = end; } // Entered a raw block? if (_isRaw) { s = new TextStatement(ConsumeTag(segment, end + 1, "endraw", out end)); index = end; } if (s != null) { _context.CurrentBlock.AddStatement(s); } } }
public DataType VisitComment(CommentStatement c) { return(DataType.Cont); }
public virtual void Visit(CommentStatement statement) { VisitSubNodes(statement); }
public SymbolTable VisitComment(CommentStatement c) { throw new NotImplementedException(); }
protected virtual void VisitCommentStatement(CommentStatement statement) { State.Write(this.CommentOpenQuote); VisitStatement(statement.Content); State.Write(this.CommentCloseQuote); }
/// <summary> /// Generates the code for a <see cref="CommentStatement"/>. /// </summary> /// <param name="statement">The statement</param> /// <returns>A BaZic code</returns> private string GenerateCommentStatement(CommentStatement statement) { return($"# {statement.Comment}"); }