parser

package module
v0.0.0-...-db5a13a Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 26, 2016 License: MIT Imports: 4 Imported by: 17 modules, 17 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

parser

Parser Combinator Library

Usage

import p "github.com/andyleap/parser"

func MakeParser() *p.Grammar {
  number := p.And(p.Mult(0, 1, p.Lit("-")), p.Mult(1, 0, p.Set("0-9")), p.Mult(0, 1, p.And(p.Lit("."), p.Mult(0, 0, p.Set("0-9")))))
  number.Node(func(m Match) (Match, error) {
		v, err := strconv.ParseFloat(String(m), 64)
		if err != nil {
			return nil, err
		}
		return v, nil
	})
  
  expr := p.And(number, p.Mult(0, 0, p.And(Set("+-"), number)))
  expr.Node(func(m Match) (Match, error) {
		mt := m.(MatchTree)
		val := mt[0].(float64)
		for _, op := range mt[1].(MatchTree) {
			switch op.(MatchTree)[0].(MatchString) {
			case "+":
				val = val + op.(MatchTree)[1].(float64)
			case "-":
				val = val - op.(MatchTree)[1].(float64)
			}
		}
		return val, nil
	})
	
	return expr
}

func main() {
  mathparser := MakeParser()
  res, _ := mathparser.ParseString("1+2")
  fmt.Println(res)
  res, _ = mathparser.ParseString("1+2-5+3.652+3")
  fmt.Println(res)
  res, _ = mathparser.ParseString("5--1")
  fmt.Println(res)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func String

func String(m Match) string

Types

type Grammar

type Grammar struct {
	// contains filtered or unexported fields
}

func And

func And(ps ...*Grammar) *Grammar

func Ignore

func Ignore(g *Grammar) *Grammar

func Lit

func Lit(text string) *Grammar

func Mult

func Mult(n, m int, p *Grammar) *Grammar

func Optional

func Optional(g *Grammar) *Grammar

func Or

func Or(ps ...*Grammar) *Grammar

func Require

func Require(ps ...*Grammar) *Grammar

func Set

func Set(set string) *Grammar

func Tag

func Tag(tag string, g *Grammar) *Grammar

func (*Grammar) Node

func (g *Grammar) Node(node func(Match) (Match, error))

func (*Grammar) Parse

func (g *Grammar) Parse(rs io.ReadSeeker) (Match, error)

func (*Grammar) ParseString

func (g *Grammar) ParseString(s string) (Match, error)

func (*Grammar) Set

func (g *Grammar) Set(ng *Grammar)

type Match

type Match interface{}

func GetTag

func GetTag(m Match, tag string) Match

func GetTags

func GetTags(m Match, tag string) []Match

func TagMatch

func TagMatch(tag string, match Match) Match

type MatchString

type MatchString string

type MatchTree

type MatchTree []Match

type TaggedMatch

type TaggedMatch struct {
	Match Match
	Tag   string
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL