From e3740eb9075594d8a2e6e026c542b27f01fe8e9b Mon Sep 17 00:00:00 2001 From: Thomas Voss Date: Thu, 18 Apr 2024 10:46:14 +0200 Subject: Add support for comments --- formatter/formatter.go | 3 +++ formatter/formatter_test.go | 27 +++++++++++++++++++++++++++ gsp.5 | 21 ++++++++++++++++++++- parser/parser.go | 4 ++-- 4 files changed, 52 insertions(+), 3 deletions(-) diff --git a/formatter/formatter.go b/formatter/formatter.go index 526de5a..1d8dd58 100644 --- a/formatter/formatter.go +++ b/formatter/formatter.go @@ -27,6 +27,9 @@ func PrintAst(ast parser.AstNode) { case parser.Text: printText(ast.Text) case parser.Normal: + if ast.Text == "/" { + return + } fmt.Printf("<%s", ast.Text) printAttrs(ast.Attrs) fmt.Print(">") diff --git a/formatter/formatter_test.go b/formatter/formatter_test.go index 4fe8035..870b904 100644 --- a/formatter/formatter_test.go +++ b/formatter/formatter_test.go @@ -252,3 +252,30 @@ func TestTrimRightSpaces(t *testing.T) { t.Fatalf("trimRightSpaces() returned ‘%s’", sy) } } + +func TestPrintAstWithComments(t *testing.T) { + s := ` + html lang="en" { + body { + / p {= Hello, Sailor!} + p {= Hello, World!} + } + }` + result := `

Hello, World!

` + + // Write the source to a temp file + r := strings.NewReader(s) + f, _ := os.CreateTemp("", "tmp*") + defer f.Close() + io.Copy(f, r) + f.Seek(0, 0) + ast, _ := parser.ParseFile(f) + + redirectStdout() + PrintAst(ast) + + out := restoreAndCapture() + if out != result { + t.Fatalf("PrintAst() printed unexpected string ‘%s’", out) + } +} diff --git a/gsp.5 b/gsp.5 index 41b458c..b193657 100644 --- a/gsp.5 +++ b/gsp.5 @@ -1,4 +1,4 @@ -.Dd $Mdocdate: November 1 2023 $ +.Dd $Mdocdate: April 18 2024 $ .Dt GSP 5 .Os .Sh NAME @@ -34,6 +34,11 @@ html lang="en" { p #my-id {- This is a paragraph with the id ‘my-id’ } p .my-cls {- This is a paragraph with the class ‘my-cls’ } + / div { + p {- This entire div is commented out. } + p {- Isn’t that neat? } + } + p #some-id .class-1 @@ -99,6 +104,20 @@ Node names follow the exact same naming rules as names do in XML. See the XML reference in .Sx SEE ALSO for more details. +.Ss Comments +Comments can be created by using the special +.Sq / +node name. +During transpilation any nodes named +.Sq / +and their children are commented out: +.Pp +.Bd -literal -offset indent +div { + / p {-I am commented out} + p {-I am not commented out} +} +.Ed .Ss Attributes Attributes are optional components of a node. They take the form of an attribute name and an optional attribute value. diff --git a/parser/parser.go b/parser/parser.go index 7d69e30..01ca6c3 100644 --- a/parser/parser.go +++ b/parser/parser.go @@ -363,9 +363,9 @@ func (reader *reader) parseString() (string, error) { } // validNameStartChar returns whether or not the rune ‘r’ is a legal rune in the -// first position an XML tag name. +// first position an XML tag name, or a slash. func validNameStartChar(r rune) bool { - return r == ':' || r == '_' || + return r == '/' || r == ':' || r == '_' || (r >= 'A' && r <= 'Z') || (r >= 'a' && r <= 'z') || (r >= 0x000C0 && r <= 0x000D6) || -- cgit v1.2.3