Code Monkey home page Code Monkey logo

peg's Introduction

PEG

peggrep

Example file demo_file:

THIS LINE IS THE 1ST UPPER CASE LINE IN THIS FILE.
this line is the 1st lower case line in this file.
This Line Has All Its First Character Of The Word With Upper Case.

Two lines above this line is empty.
And this is the last line.

Match literals

$ peggrep "'this'" demo_file
this line is the 1st lower case line in this file.
Two lines above this line is empty.
And this is the last line.
  • 'this': match the literal string "this"

Match this.*empty

$ peggrep "'this' (!'empty' .)* 'empty'" demo_file
Two lines above this line is empty.
  • !'empty':
    • def: true if the current position is not followed by "empty"
    • ! does not consume any characters
  • .: consume one character unconditionally
  • (!'empty' .)*:
    • def: match any number of characters that are not followed by "empty"
    • when the position is at the e of "empty", the (!'empty' .)* exits
    • why not just .* 'empty':
      • * is greedy in PEG
      • .* will consume "empty" without exiting, so 'empty' has no chance to match
  • The final 'empty': match and consume the "empty"

To make life easier, we can make (!'empty' .)* 'empty' into a function:

  1. Write a grammar file:
    // grammar.peg
    until[str] <- (!str .)* str
                ;
  2. Run with the grammar file:
    $ peggrep -g grammar.peg "'this' until['empty']" demo_file
    Two lines above this line is empty.

Match digits

Steps:

  1. Write a grammar file:
    // grammar.peg
    digit <- '0' / '1' / '2' / '3' / '4' / '5' / '6' / '7' / '8' / '9'
           ;
  2. Run with the grammar file:
    $ peggrep -g grammar.peg "digit+" demo_file
    THIS LINE IS THE 1ST UPPER CASE LINE IN THIS FILE.
    this line is the 1st lower case line in this file.

Grammar file from environment variable

We don't want to write a grammar file every time we want to use it. We can use the environment variable PEGGREP_GRAMMAR to specify the grammar file:

$ export PEGGREP_GRAMMAR="/absolute/path/to/grammar.peg"
$ peggrep "'this' until['empty']" demo_file
Two lines above this line is empty.

Match all private functions

Steps:

  1. Write a grammar file:
    // grammar.peg
    space <- ' ' / '\t' / '\r' / '\n'
           ;
    _     <- space*
           ;
  2. Run:
    $ export PEGGREP_GRAMMAR="/absolute/path/to/grammar.peg"
    $ peggrep -^ "_ !'pub' 'fn'" src/*.rs
    • -^: match at the start of a line
    • _:
      • predefined in the grammar file
      • to match any number of spaces

peg's People

Contributors

banyc avatar

Stargazers

 avatar  avatar  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.