Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: json example #395

Merged
merged 2 commits into from Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
86 changes: 86 additions & 0 deletions _examples/json/main.go
@@ -0,0 +1,86 @@
// nolint: golint, dupl
package main

import (
"os"

"github.com/alecthomas/kong"

"github.com/alecthomas/participle/v2"
"github.com/alecthomas/participle/v2/lexer"
)

var (
jsonLexer = lexer.MustSimple([]lexer.SimpleRule{
{Name: "Comment", Pattern: `\/\/[^\n]*`},
{Name: "String", Pattern: `"(\\"|[^"])*"`},
{Name: "Number", Pattern: `[-+]?(\d*\.)?\d+`},
zhuliquan marked this conversation as resolved.
Show resolved Hide resolved
{Name: "Punct", Pattern: `[-[!@#$%^&*()+_={}\|:;"'<,>.?/]|]`},
{Name: "Null", Pattern: "null"},
{Name: "True", Pattern: "true"},
{Name: "False", Pattern: "false"},
{Name: "EOL", Pattern: `[\n\r]+`},
{Name: "Whitespace", Pattern: `[ \t\n\r]+`},
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

\n\r will never match here because EOL takes precedence.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed it

})

jsonParser = participle.MustBuild[Json](
participle.Lexer(jsonLexer),
participle.Unquote("String"),
participle.Elide("Whitespace", "EOL"),
participle.UseLookahead(2),
)

cli struct {
File string `arg:"" type:"existingfile" help:"File to parse."`
}
)

// Parse a Json string.
func Parse(data []byte) (*Json, error) {
json, err := jsonParser.ParseBytes("", data)
if err != nil {
return nil, err
}
return json, nil
}

type Json struct {
Pos lexer.Position

Object *Object `parser:"@@ |"`
Array *Array `parser:"@@ |"`
Number *string `parser:"@Number |"`
String *string `parser:"@String |"`
False *string `parser:"@False |"`
True *string `parser:"@True |"`
Null *string `parser:"@Null"`
}

type Object struct {
Pos lexer.Position

Pairs []*Pair `parser:"'{' @@ (',' @@)* '}'"`
}

type Pair struct {
Pos lexer.Position

Key string `parser:"@String ':'"`
Value *Json `parser:"@@"`
}

type Array struct {
Pos lexer.Position

Items []*Json `parser:"'[' @@ (',' @@)* ']'"`
}

func main() {
ctx := kong.Parse(&cli)
data, err := os.ReadFile(cli.File)
ctx.FatalIfErrorf(err)

res, err := Parse(data)
ctx.FatalIfErrorf(err)
ctx.Printf("res is: %v", res)
}
15 changes: 15 additions & 0 deletions _examples/json/main_test.go
@@ -0,0 +1,15 @@
package main

import (
"os"
"testing"

require "github.com/alecthomas/assert/v2"
)

func TestParse(t *testing.T) {
src, err := os.ReadFile("./test.json")
require.NoError(t, err)
_, err = Parse(src)
require.NoError(t, err)
}
11 changes: 11 additions & 0 deletions _examples/json/test.json
@@ -0,0 +1,11 @@
{
"list": [1, 1.2, 1, -1, {"foo": "bar"}, true, false, null],
"object": {
"foo1": "bar2",
"foo2": true,
"foo3": false,
"foo4": null,
"foo5": 1,
"foo6": "ss"
}
}