GithubHelp home page GithubHelp logo

cch123 / peg Goto Github PK

View Code? Open in Web Editor NEW

This project forked from pointlander/peg

1.0 2.0 0.0 548 KB

Peg, Parsing Expression Grammar, is an implementation of a Packrat parser generator.

License: BSD 3-Clause "New" or "Revised" License

Go 100.00%

peg's Introduction

About

Peg, Parsing Expression Grammar, is an implementation of a Packrat parser generator. A Packrat parser is a descent recursive parser capable of backtracking. The generated parser searches for the correct parsing of the input.

For more information see:

This Go implementation is based on:

Development

To rebuild from scratch:

go run build.go

For full test:

go run build.go test

Usage

-inline
 Tells the parser generator to inline parser rules.
-switch
 Use at your own peril!
 Reduces the number of rules that have to be tried for some pegs.
 If statements are replaced with switch statements.

Syntax

First declare the package name and any import(s) required:

package <package name>

import <import name>

Then declare the parser:

type <parser name> Peg {
	<parser state variables>
}

Next declare the rules. The first rule is the entry point into the parser:

<rule name> <- <rule body>

The first rule should probably end with !. to indicate no more input follows:

first <- . !.

. means any character matches. For zero or more character matches, use:

repetition <- .*

For one or more character matches, use:

oneOrMore <- .+

For an optional character match, use:

optional <- .?

If specific characters are to be matched, use single quotes:

specific <- 'a'* 'bc'+ 'de'?

will match the string "aaabcbcde".

For choosing between different inputs, use alternates:

prioritized <- 'a' 'a'* / 'bc'+ / 'de'?

will match "aaaa" or "bcbc" or "de" or "". The matches are attempted in order.

If the characters are case insensitive, use double quotes:

insensitive <- "abc"

will match "abc" or "Abc" or "ABc" etc...

For matching a set of characters, use a character class:

class <- [a-z]

will match "a" or "b" or all the way to "z".

For an inverse character class, start with a caret:

inverse <- [^a-z]

will match anything but "a" or "b" or all the way to "z".

If the character class is case insensitive, use double brackets:

insensitive <- [[A-Z]]

Use parentheses for grouping:

grouping <- (rule1 / rule2) rule3

For looking ahead a match (predicate), use:

lookAhead <- &rule1 rule2

For inverse look ahead, use:

inverse <- !rule1 rule2

Use curly braces for Go code:

gocode <- { fmt.Println("hello world") }

For string captures, use less than and greater than:

capture <- <'capture'> { fmt.Println(buffer[begin:end]) }

Will print out "capture". The captured string is stored in buffer[begin:end].

Files

  • bootstrap/main.go: bootstrap syntax tree of peg
  • tree/peg.go: syntax tree and code generator
  • peg.peg: peg in its own language

Author

Andrew Snodgrass

peg's People

Contributors

abhinav avatar amery avatar art4711 avatar dimus avatar egorse avatar elireisman avatar fd0 avatar gkrasin avatar haskellcamargo avatar heyitsanthony avatar ivanhernandez avatar karlll avatar kimshrier avatar masatake avatar maverickwoo avatar nilium avatar pointlander avatar sethwklein avatar spazm avatar taruti avatar tgpfeiffer avatar tmc avatar vvakame avatar xaviershay avatar zeslava avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.