Skip to content

Commit

Permalink
Merge pull request #11 from opsidian/add_schema
Browse files Browse the repository at this point in the history
add schema
  • Loading branch information
bandesz authored Apr 16, 2021
2 parents ebfe5fb + 8b7af1e commit fa5058b
Show file tree
Hide file tree
Showing 54 changed files with 565 additions and 629 deletions.
1 change: 0 additions & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ jobs:
steps:
- checkout
- run: make test
- run: bash <(curl -s https://codecov.io/bash)

workflows:
version: 2
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Parsley - Parser combinator library written in Go

[![CircleCI status](https://circleci.com/gh/opsidian/parsley.svg?style=shield&circle-token=c42cce0e1ae1496645d1d6dc640d86a9e6de808d)](https://circleci.com/gh/opsidian/parsley) [![Codecov.io status](https://codecov.io/gh/opsidian/parsley/branch/master/graph/badge.svg)](https://codecov.io/gh/opsidian/parsley) [![GoDoc](https://godoc.org/github.com/opsidian/parsley?status.svg)](https://godoc.org/github.com/opsidian/parsley) [![Latest release](https://img.shields.io/github/release/opsidian/parsley.svg)](https://github.com/opsidian/parsley/releases/latest)
[![CircleCI status](https://circleci.com/gh/opsidian/parsley.svg?style=shield&circle-token=c42cce0e1ae1496645d1d6dc640d86a9e6de808d)](https://circleci.com/gh/opsidian/parsley) [![GoDoc](https://godoc.org/github.com/opsidian/parsley?status.svg)](https://godoc.org/github.com/opsidian/parsley) [![Latest release](https://img.shields.io/github/release/opsidian/parsley.svg)](https://github.com/opsidian/parsley/releases/latest)

Parsley is a general parser combinator library which can be used to parse context-free, left-recursive languages. It handles indirect as well as direct left-recursion in polynomial time and defines a memoization helper for speeding up parsing time. The language grammar can be easily translated to a set of rules using parsers and combinators.

Expand Down
128 changes: 64 additions & 64 deletions ast/astfakes/fake_reader_pos_setter_node.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions ast/empty_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ func (e EmptyNode) Token() string {
return "EMPTY"
}

// Type returns with an empty type
func (e EmptyNode) Type() string {
return ""
// Schema returns nil
func (e EmptyNode) Schema() interface{} {
return nil
}

// Value returns with nil
Expand Down
4 changes: 0 additions & 4 deletions ast/empty_node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,6 @@ var _ = Describe("EmptyNode", func() {
Expect(err).ToNot(HaveOccurred())
})

It("Type() should return empty", func() {
Expect(node.Type()).To(BeEmpty())
})

It("Pos() should return with the token value", func() {
Expect(node.Pos()).To(Equal(pos))
})
Expand Down
4 changes: 2 additions & 2 deletions ast/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ var _ = Describe("AppendNode", func() {

var _ = Describe("SetReaderPos", func() {
It("calls the SetReaderPos function if node implements the SetReaderPos interface", func() {
node := ast.NewTerminalNode("TEST", "x", "string", parsley.Pos(1), parsley.Pos(2))
node := ast.NewTerminalNode("string", "TEST", "x", parsley.Pos(1), parsley.Pos(2))
f := func(pos parsley.Pos) parsley.Pos {
return parsley.Pos(pos + 1)
}
Expand All @@ -80,7 +80,7 @@ var _ = Describe("SetReaderPos", func() {
})

It("sets the reader position for all nodes in a node list", func() {
n1 := ast.NewTerminalNode("TEST", "x", "string", parsley.Pos(1), parsley.Pos(2))
n1 := ast.NewTerminalNode("string", "TEST", "x", parsley.Pos(1), parsley.Pos(2))
n2 := ast.EmptyNode(parsley.Pos(3))

nl := ast.NodeList([]parsley.Node{n1, n2})
Expand Down
4 changes: 2 additions & 2 deletions ast/interpreter/interpreter.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ type selectInterpreter struct {
}

// StaticCheck runs the static checking on the indexed node
func (s selectInterpreter) StaticCheck(userCtx interface{}, node parsley.NonTerminalNode) (string, parsley.Error) {
func (s selectInterpreter) StaticCheck(userCtx interface{}, node parsley.NonTerminalNode) (interface{}, parsley.Error) {
nodes := node.Children()
if s.i < 0 || s.i >= len(nodes) {
panic(fmt.Sprintf("node index is out of bounds: %d", s.i))
}
return nodes[s.i].Type(), nil
return nodes[s.i].Schema(), nil
}

func (s selectInterpreter) Eval(userCtx interface{}, node parsley.NonTerminalNode) (interface{}, parsley.Error) {
Expand Down
2 changes: 1 addition & 1 deletion ast/interpreter/interpreter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ var _ = Describe("Interpreter", func() {
ctx = "context"
node1 = &parsleyfakes.FakeNode{}
node1.ValueReturns(1, parsley.NewErrorf(parsley.Pos(1), "err1"))
node1.TypeReturns("testtype")
node1.SchemaReturns("testtype")
node2 = &parsleyfakes.FakeNode{}
node2.ValueReturns(2, parsley.NewErrorf(parsley.Pos(2), "err2"))
node = &parsleyfakes.FakeNonTerminalNode{}
Expand Down
6 changes: 3 additions & 3 deletions ast/node_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ func (nl NodeList) Token() string {
return nl[0].Token()
}

// Type returns with the type of the first node
func (nl NodeList) Type() string {
return nl[0].Type()
// Schema returns nil
func (nl NodeList) Schema() interface{} {
return nil
}

// Value returns with the value of the first node
Expand Down
25 changes: 0 additions & 25 deletions ast/node_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,31 +60,6 @@ var _ = Describe("NodeList", func() {
})
})

Describe("Type", func() {
Context("when empty", func() {
BeforeEach(func() {
nl = ast.NodeList([]parsley.Node{})
})
It("should panic", func() {
Expect(func() { nl.Type() }).To(Panic())
})
})

Context("when not empty", func() {
BeforeEach(func() {
nl = ast.NodeList([]parsley.Node{n1, n2})
})
It("should return the reader pos of the first item", func() {
n1.TypeReturns("testtype")

Expect(nl.Type()).To(Equal("testtype"))

Expect(n1.TypeCallCount()).To(Equal(1))
Expect(n2.TypeCallCount()).To(Equal(0))
})
})
})

Describe("Value", func() {
Context("when empty", func() {
BeforeEach(func() {
Expand Down
17 changes: 7 additions & 10 deletions ast/nonterminal_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import (

// NonTerminalNode represents a branch node in the AST
type NonTerminalNode struct {
schema interface{}
token string
valueType string
children []parsley.Node
pos parsley.Pos
readerPos parsley.Pos
Expand Down Expand Up @@ -57,9 +57,9 @@ func (n *NonTerminalNode) Token() string {
return n.token
}

// Type returns with the type of the node's value
func (n *NonTerminalNode) Type() string {
return n.valueType
// Schema returns the schema for the node's value
func (n *NonTerminalNode) Schema() interface{} {
return n.schema
}

// Value returns with the value of the node
Expand Down Expand Up @@ -95,11 +95,11 @@ func (n *NonTerminalNode) StaticCheck(userCtx interface{}) parsley.Error {
if n.interpreter != nil {
switch i := n.interpreter.(type) {
case parsley.StaticChecker:
valueType, err := i.StaticCheck(userCtx, n)
schema, err := i.StaticCheck(userCtx, n)
if err != nil {
return err
}
n.valueType = valueType
n.schema = schema
}
}

Expand Down Expand Up @@ -128,8 +128,5 @@ func (n *NonTerminalNode) SetReaderPos(f func(parsley.Pos) parsley.Pos) {

// String returns with a string representation of the node
func (n *NonTerminalNode) String() string {
if n.valueType == "" {
return fmt.Sprintf("%s{%s, %d..%d}", n.token, n.children, n.pos, n.readerPos)
}
return fmt.Sprintf("%s{<%s> %s, %d..%d}", n.token, n.valueType, n.children, n.pos, n.readerPos)
return fmt.Sprintf("%s{%s, %d..%d}", n.token, n.children, n.pos, n.readerPos)
}
Loading

0 comments on commit fa5058b

Please sign in to comment.