• Stars
    star
    403
  • Rank 107,140 (Top 3 %)
  • Language
    Ruby
  • Created over 14 years ago
  • Updated over 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Parsing Expressions for Ruby

Citrus :: Parsing Expressions for Ruby

Citrus is a compact and powerful parsing library for Ruby that combines the elegance and expressiveness of the language with the simplicity and power of parsing expressions.

Installation

Via RubyGems:

$ gem install citrus

From a local copy:

$ git clone git://github.com/mjackson/citrus.git
$ cd citrus
$ rake package install

Background

In order to be able to use Citrus effectively, you must first understand the difference between syntax and semantics. Syntax is a set of rules that govern the way letters and punctuation may be used in a language. For example, English syntax dictates that proper nouns should start with a capital letter and that sentences should end with a period.

Semantics are the rules by which meaning may be derived in a language. For example, as you read a book you are able to make some sense of the particular way in which words on a page are combined to form thoughts and express ideas because you understand what the words themselves mean and you understand what they mean collectively.

Computers use a similar process when interpreting code. First, the code must be parsed into recognizable symbols or tokens. These tokens may then be passed to an interpreter which is responsible for forming actual instructions from them.

Citrus is a pure Ruby library that allows you to perform both lexical analysis and semantic interpretation quickly and easily. Using Citrus you can write powerful parsers that are simple to understand and easy to create and maintain.

In Citrus, there are three main types of objects: rules, grammars, and matches.

Rules

A Rule is an object that specifies some matching behavior on a string. There are two types of rules: terminals and non-terminals. Terminals can be either Ruby strings or regular expressions that specify some input to match. For example, a terminal created from the string "end" would match any sequence of the characters "e", "n", and "d", in that order. Terminals created from regular expressions may match any sequence of characters that can be generated from that expression.

Non-terminals are rules that may contain other rules but do not themselves match directly on the input. For example, a Repeat is a non-terminal that may contain one other rule that will try and match a certain number of times. Several other types of non-terminals are available that will be discussed later.

Rule objects may also have semantic information associated with them in the form of Ruby modules. Rules use these modules to extend the matches they create.

Grammars

A Grammar is a container for rules. Usually the rules in a grammar collectively form a complete specification for some language, or a well-defined subset thereof.

A Citrus grammar is really just a souped-up Ruby module. These modules may be included in other grammar modules in the same way that Ruby modules are normally used. This property allows you to divide a complex grammar into more manageable, reusable pieces that may be combined at runtime. Any rule with the same name as a rule in an included grammar may access that rule with a mechanism similar to Ruby's super keyword.

Matches

A Match object represents a successful recognition of some piece of the input. Matches are created by rule objects during a parse.

Matches are arranged in a tree structure where any match may contain any number of other matches. Each match contains information about its own subtree. The structure of the tree is determined by the way in which the rule that generated each match is used in the grammar. For example, a match that is created from a nonterminal rule that contains several other terminals will likewise contain several matches, one for each terminal. However, this is an implementation detail and should be relatively transparent to the user.

Match objects may be extended with semantic information in the form of methods. These methods should provide various interpretations for the semantic value of a match.

Syntax

The most straightforward way to compose a Citrus grammar is to use Citrus' own custom grammar syntax. This syntax borrows heavily from Ruby, so it should already be familiar to Ruby programmers.

Terminals

Terminals may be represented by a string or a regular expression. Both follow the same rules as Ruby string and regular expression literals.

'abc'         # match "abc"
"abc\n"       # match "abc\n"
/abc/i        # match "abc" in any case
/\xFF/        # match "\xFF"

Character classes and the dot (match anything) symbol are supported as well for compatibility with other parsing expression implementations.

[a-z0-9]      # match any lowercase letter or digit
[\x00-\xFF]   # match any octet
.             # match any single character, including new lines

Also, strings may use backticks instead of quotes to indicate that they should match in a case-insensitive manner.

`abc`         # match "abc" in any case

Besides case sensitivity, case-insensitive strings have the same behavior as double quoted strings.

See Terminal and StringTerminal for more information.

Repetition

Quantifiers may be used after any expression to specify a number of times it must match. The universal form of a quantifier is N*M where N is the minimum and M is the maximum number of times the expression may match.

'abc'1*2      # match "abc" a minimum of one, maximum of two times
'abc'1*       # match "abc" at least once
'abc'*2       # match "abc" a maximum of twice

Additionally, the minimum and maximum may be omitted entirely to specify that an expression may match zero or more times.

'abc'*        # match "abc" zero or more times

The + and ? operators are supported as well for the common cases of 1* and *1 respectively.

'abc'+        # match "abc" one or more times
'abc'?        # match "abc" zero or one time

See Repeat for more information.

Lookahead

Both positive and negative lookahead are supported in Citrus. Use the & and ! operators to indicate that an expression either should or should not match. In neither case is any input consumed.

'a' &'b'      # match an "a" that is followed by a "b"
'a' !'b'      # match an "a" that is not followed by a "b"
!'a' .        # match any character except for "a"

A special form of lookahead is also supported which will match any character that does not match a given expression.

~'a'          # match all characters until an "a"
~/xyz/        # match all characters until /xyz/ matches

When using this operator (the tilde), at least one character must be consumed for the rule to succeed.

See AndPredicate, NotPredicate, and ButPredicate for more information.

Sequences

Sequences of expressions may be separated by a space to indicate that the rules should match in that order.

'a' 'b' 'c'   # match "a", then "b", then "c"
'a' [0-9]     # match "a", then a numeric digit

See Sequence for more information.

Choices

Ordered choice is indicated by a vertical bar that separates two expressions. When using choice, each expression is tried in order. When one matches, the rule returns the match immediately without trying the remaining rules.

'a' | 'b'       # match "a" or "b"
'a' 'b' | 'c'   # match "a" then "b" (in sequence), or "c"

It is important to note when using ordered choice that any operator binds more tightly than the vertical bar. A full chart of operators and their respective levels of precedence is below.

See Choice for more information.

Labels

Match objects may be referred to by a different name than the rule that originally generated them. Labels are added by placing the label and a colon immediately preceding any expression.

chars:/[a-z]+/  # the characters matched by the regular expression
                # may be referred to as "chars" in an extension
                # method

Extensions

Extensions may be specified using either "module" or "block" syntax. When using module syntax, specify the name of a module that is used to extend match objects in between less than and greater than symbols.

[a-z0-9]5*9 <CouponCode>  # match a string that consists of any lower
                          # cased letter or digit between 5 and 9
                          # times and extend the match with the
                          # CouponCode module

Additionally, extensions may be specified inline using curly braces. When using this method, the code inside the curly braces may be invoked by calling the value method on the match object.

[0-9] { to_str.to_i }     # match any digit and return its integer value when
                          # calling the #value method on the match object

Note that when using the inline block method you may also specify arguments in between vertical bars immediately following the opening curly brace, just like in Ruby blocks.

Super

When including a grammar inside another, all rules in the child that have the same name as a rule in the parent also have access to the super keyword to invoke the parent rule.

grammar Number
  rule number
    [0-9]+
  end
end

grammar FloatingPoint
  include Number

  rule number
    super ('.' super)?
  end
end

In the example above, the FloatingPoint grammar includes Number. Both have a rule named number, so FloatingPoint#number has access to Number#number by means of using super.

See Super for more information.

Precedence

The following table contains a list of all Citrus symbols and operators and their precedence. A higher precedence indicates tighter binding.

Operator Name Precedence
'' String (single quoted) 7
"" String (double quoted) 7
`` String (case insensitive) 7
[] Character class 7
. Dot (any character) 7
// Regular expression 7
() Grouping 7
* Repetition (arbitrary) 6
+ Repetition (one or more) 6
? Repetition (zero or one) 6
& And predicate 5
! Not predicate 5
~ But predicate 5
<> Extension (module name) 4
{} Extension (literal) 4
: Label 3
e1 e2 Sequence 2
e1 | e2 Ordered choice 1

Grouping

As is common in many programming languages, parentheses may be used to override the normal binding order of operators. In the following example parentheses are used to make the vertical bar between 'b' and 'c' bind tighter than the space between 'a' and 'b'.

'a' ('b' | 'c')   # match "a", then "b" or "c"

Example

Below is an example of a simple grammar that is able to parse strings of integers separated by any amount of white space and a + symbol.

grammar Addition
  rule additive
    number plus (additive | number)
  end

  rule number
    [0-9]+ space
  end

  rule plus
    '+' space
  end

  rule space
    [ \t]*
  end
end

Several things to note about the above example:

  • Grammar and rule declarations end with the end keyword
  • A sequence of rules is created by separating expressions with a space
  • Likewise, ordered choice is represented with a vertical bar
  • Parentheses may be used to override the natural binding order
  • Rules may refer to other rules in their own definitions simply by using the other rule's name
  • Any expression may be followed by a quantifier

Interpretation

The grammar above is able to parse simple mathematical expressions such as "1+2" and "1 + 2+3", but it does not have enough semantic information to be able to actually interpret these expressions.

At this point, when the grammar parses a string it generates a tree of Match objects. Each match is created by a rule and may itself be comprised of any number of submatches.

Submatches are created whenever a rule contains another rule. For example, in the grammar above number matches a string of digits followed by white space. Thus, a match generated by this rule will contain two submatches.

We can define a method inside a set of curly braces that will be used to extend a particular rule's matches. This works in similar fashion to using Ruby's blocks. Let's extend the Addition grammar using this technique.

grammar Addition
  rule additive
    (number plus term:(additive | number)) {
      capture(:number).value + capture(:term).value
    }
  end

  rule number
    ([0-9]+ space) {
      to_str.to_i
    }
  end

  rule plus
    '+' space
  end

  rule space
    [ \t]*
  end
end

In this version of the grammar we have added two semantic blocks, one each for the additive and number rules. These blocks contain code that we can execute by calling value on match objects that result from those rules. It's easiest to explain what is going on here by starting with the lowest level block, which is defined within number.

Inside this block we see a call to another method, namely to_str. When called in the context of a match object, this method returns the match's internal string object. Thus, the call to to_str.to_i should return the integer value of the match.

Similarly, matches created by additive will also have a value method. Notice the use of the term label within the rule definition. This label allows the match that is created by the choice between additive and number to be retrieved using capture(:term). The value of an additive match is determined to be the values of its number and term matches added together using Ruby's addition operator. Note that the plural form captures(:term) can be used to get an array of matches for a given label (e.g. when the label belongs to a repetition).

Since additive is the first rule defined in the grammar, any match that results from parsing a string with this grammar will have a value method that can be used to recursively calculate the collective value of the entire match tree.

To give it a try, save the code for the Addition grammar in a file called addition.citrus. Next, assuming you have the Citrus gem installed, try the following sequence of commands in a terminal.

$ irb
> require 'citrus'
 => true
> Citrus.load 'addition'
 => [Addition]
> m = Addition.parse '1 + 2 + 3'
 => #<Citrus::Match ...
> m.value
 => 6

Congratulations! You just ran your first piece of Citrus code.

One interesting thing to notice about the above sequence of commands is the return value of Citrus#load. When you use Citrus.load to load a grammar file (and likewise Citrus#eval to evaluate a raw string of grammar code), the return value is an array of all the grammars present in that file.

Take a look at calc.citrus for an example of a calculator that is able to parse and evaluate more complex mathematical expressions.

Additional Methods

If you need more than just a value method on your match object, you can attach additional methods as well. There are two ways to do this. The first lets you define additional methods inline in your semantic block. This block will be used to create a new Module using Module#new. Using the Addition example above, we might refactor the additive rule to look like this:

rule additive
  (number plus term:(additive | number)) {
    def lhs
      capture(:number).value
    end

    def rhs
      capture(:term).value
    end

    def value
      lhs + rhs
    end
  }
end

Now, in addition to having a value method, matches that result from the additive rule will have a lhs and a rhs method as well. Although not particularly useful in this example, this technique can be useful when unit testing more complex rules. For example, using this method you might make the following assertions in a unit test:

match = Addition.parse('1 + 4')
assert_equal(1, match.lhs)
assert_equal(4, match.rhs)
assert_equal(5, match.value)

If you would like to abstract away the code in a semantic block, simply create a separate Ruby module (in another file) that contains the extension methods you want and use the angle bracket notation to indicate that a rule should use that module when extending matches.

To demonstrate this method with the above example, in a Ruby file you would define the following module.

module Additive
  def lhs
    capture(:number).value
  end

  def rhs
    capture(:term).value
  end

  def value
    lhs + rhs
  end
end

Then, in your Citrus grammar file the rule definition would look like this:

  rule additive
    (number plus term:(additive | number)) <Additive>
  end

This method of defining extensions can help keep your grammar files cleaner. However, you do need to make sure that your extension modules are already loaded before using Citrus.load to load your grammar file.

Testing

Citrus was designed to facilitate simple and powerful testing of grammars. To demonstrate how this is to be done, we'll use the Addition grammar from our previous example. The following code demonstrates a simple test case that could be used to test that our grammar works properly.

class AdditionTest < Test::Unit::TestCase
  def test_additive
    match = Addition.parse('23 + 12', :root => :additive)
    assert(match)
    assert_equal('23 + 12', match)
    assert_equal(35, match.value)
  end

  def test_number
    match = Addition.parse('23', :root => :number)
    assert(match)
    assert_equal('23', match)
    assert_equal(23, match.value)
  end
end

The key here is using the :root option when performing the parse to specify the name of the rule at which the parse should start. In test_number, since :number was given the parse will start at that rule as if it were the root rule of the entire grammar. The ability to change the root rule on the fly like this enables easy unit testing of the entire grammar.

Also note that because match objects are themselves strings, assertions may be made to test equality of match objects with string values.

Debugging

When a parse fails, a ParseError object is generated which provides a wealth of information about exactly where the parse failed including the offset, line number, line text, and line offset. Using this object, you could possibly provide some useful feedback to the user about why the input was bad. The following code demonstrates one way to do this.

def parse_some_stuff(stuff)
  match = StuffGrammar.parse(stuff)
rescue Citrus::ParseError => e
  raise ArgumentError, "Invalid stuff on line %d, offset %d!" %
    [e.line_number, e.line_offset]
end

In addition to useful error objects, Citrus also includes a means of visualizing match trees in the console via Match#dump. This can help when determining which rules are generating which matches and how they are organized in the match tree.

Extras

Several files are included in the Citrus repository that make it easier to work with grammar files in various editors.

TextMate

To install the Citrus TextMate bundle, simply double-click on the Citrus.tmbundle file in the extras directory.

Vim

To install the Vim scripts, copy the files in extras/vim to a directory in Vim's runtimepath.

Examples

The project source directory contains several example scripts that demonstrate how grammars are to be constructed and used. Each Citrus file in the examples directory has an accompanying Ruby file that contains a suite of tests for that particular file.

The best way to run any of these examples is to pass the name of the Ruby file directly to the Ruby interpreter on the command line, e.g.:

$ ruby -Ilib examples/calc_test.rb

This particular invocation uses the -I flag to ensure that you are using the version of Citrus that was bundled with that particular example file (i.e. the version that is contained in the lib directory).

Links

Discussion around Citrus happens on the citrus-users Google group.

The primary resource for all things to do with parsing expressions can be found on the original Packrat and Parsing Expression Grammars page at MIT.

Also, a useful summary of parsing expression grammars can be found on Wikipedia.

Citrus draws inspiration from another Ruby library for writing parsing expression grammars, Treetop. While Citrus' syntax is similar to that of Treetop, it's not identical. The link is included here for those who may wish to explore an alternative implementation.

License

Copyright 2010-2011 Michael Jackson

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

The software is provided "as is", without warranty of any kind, express or implied, including but not limited to the warranties of merchantability, fitness for a particular purpose and non-infringement. In no event shall the authors or copyright holders be liable for any claim, damages or other liability, whether in an action of contract, tort or otherwise, arising from, out of or in connection with the software or the use or other dealings in the software.

More Repositories

1

unpkg

The CDN for everything on npm
JavaScript
2,989
star
2

expect

Write better assertions
JavaScript
2,291
star
3

mach

HTTP for JavaScript
JavaScript
812
star
4

http-client

Compose HTTP clients using JavaScript's fetch API
JavaScript
506
star
5

web-starter

Build and deploy a React website quickly on Heroku
JavaScript
417
star
6

strata

A modular, streaming HTTP server for node.js
JavaScript
365
star
7

then-redis

A fast, promise-based Redis client for node.js
JavaScript
315
star
8

shadowbox

A beautiful, versatile lightbox for photos and videos
JavaScript
296
star
9

rollup-plugin-url-resolve

Use URLs in your Rollup imports
JavaScript
136
star
10

react-loop-2019

Notes and code examples from my React Loop 2019 Keynote
JavaScript
116
star
11

resolve-pathname

Resolve URL pathnames using JavaScript
JavaScript
63
star
12

expect-element

Write better assertions for DOM nodes
JavaScript
61
star
13

my-react

An experimental drop-in replacement for React without ES6 classes or "this"
JavaScript
58
star
14

dotfiles

My dotfiles
Vim Script
57
star
15

unpkg-demos

Experiments in how to use unpkg
HTML
54
star
16

history-server

An HTTP server for single-page apps that use the HTML5 history API
JavaScript
52
star
17

rack-accept

HTTP Accept* for Ruby/Rack
Ruby
46
star
18

mint

A small, fast documentation generator for literate-style programming
JavaScript
41
star
19

sinatra-session

Simple, secure sessions for Sinatra
Ruby
38
star
20

firework

A distributed, fault-tolerant work queue for Firebase
JavaScript
38
star
21

s3-thumb-server

Serve thumbnails of images stored on S3 over HTTP
JavaScript
35
star
22

ember-firebase

Firebase bindings for Ember.js
JavaScript
32
star
23

value-equal

Are these two JavaScript values equal?
JavaScript
31
star
24

monterey

Minimal OOP for JavaScript
JavaScript
31
star
25

optionparser

Command-line option parser for PHP
PHP
29
star
26

bufferedstream

A robust stream implementation for node.js and the browser
JavaScript
27
star
27

tree-shaking-react-router

Tree-shaking React Router with webpack 4
JavaScript
25
star
28

icare

A badge to promote empathy in software design, craftsmanship, and maintenance
HTML
25
star
29

react-style

Declarative styling for React components
JavaScript
21
star
30

remix-ssg-example

Small example of how to statically generate a Remix site using wget
TypeScript
19
star
31

dropbox-client

Dropbox API v2 client for JavaScript
JavaScript
17
star
32

bencode

Bencode library for PHP
PHP
15
star
33

reactjsday-2018

Demo + slides from my ReactJS Day 2018 keynote in Verona, Italy
JavaScript
14
star
34

symboltable

A Symbols-only Hash for Ruby
Ruby
13
star
35

broccoli-rev

A Broccoli plugin for adding revision checksums to file names
JavaScript
13
star
36

Fullscreen

A fullscreen sample app for Cocoa with MacRuby
Ruby
12
star
37

yuicompressor

A YUI JavaScript and CSS compressor wrapper for Ruby and JRuby
Ruby
12
star
38

react-router-examples

Code examples for React Router v6
TypeScript
12
star
39

usererror

A base class for JavaScript errors
JavaScript
12
star
40

plural

Pluralization library for PHP
PHP
12
star
41

grand

Generate random data in JavaScript
JavaScript
12
star
42

babel-plugin-import-visitor

Visit all import sources in Babel
JavaScript
10
star
43

then-couchdb

A promise-based CouchDB client for node.js
JavaScript
9
star
44

mwrc2012

Sample code from my talk at MWRC 2012
Ruby
8
star
45

multipart-web-stream

A streaming parser for multipart fetch streams
TypeScript
8
star
46

chatter

A little chat server for #CodeClass
JavaScript
6
star
47

markdown

Markdown as a Service
Ruby
5
star
48

http-client-cookie-jar

Cookie jar middleware for http-client
JavaScript
5
star
49

stratajs.org

The web site for the Strata web framework
JavaScript
5
star
50

form-data-parser

A request.formData() wrapper with pluggable file upload handling
TypeScript
5
star
51

maker

Small library for quickly constructing large segments of HTML in JavaScript
JavaScript
5
star
52

fetch-super-headers

A suite of tools that make it a little more fun to work with Headers
TypeScript
4
star
53

sinatra-spec

Simple specs for Sinatra apps using MiniTest
Ruby
3
star
54

broccoli-select

A Broccoli plugin for selecting files based on glob patterns
JavaScript
3
star
55

jview

A subclass of jQuery.fn for creating views in JavaScript
JavaScript
3
star
56

sequel-factory

Simple, powerful factories for Sequel models
Ruby
3
star
57

describe-property

Define JavaScript object properties quickly with ES5 defaults
JavaScript
3
star
58

mwrc2010

Rack For Web Developers - My presentation from MountainWest RubyConf 2010
Ruby
2
star
59

esbuild-node-builtins-sideeffects

JavaScript
2
star
60

rollup-watch-dir

JavaScript
2
star
61

html5-devconf-may2014

The slides and examples from my presentation at HTML5 DevConf on May 22, 2014
JavaScript
2
star
62

react-router-monorepo-stub

JavaScript
2
star
63

esbuild-empty-file-test

JavaScript
2
star
64

rollup-plugin-babel-bug

JavaScript
2
star
65

rubyconf2010

Grammars, Parsers, and Interpreters. In Ruby. ~ My presentation from RubyConf 2010
Ruby
2
star
66

lazy-file

Lazy, streaming files for JavaScript
TypeScript
2
star
67

jquery-pop

Painless views as models for jQuery
1
star
68

jdrag

Simple drag and drop for jQuery
JavaScript
1
star
69

redemption-from-callback-hell

JavaScript
1
star
70

mjackson.me

JavaScript
1
star
71

file-system

A simple filesystem for JavaScript, based on the File and Streams APIs
TypeScript
1
star
72

JavaScriptObjectsAndPatterns

Some code examples for a class I did for Twitter's HackWeek Q4 2012
JavaScript
1
star
73

mwrc2011

My presentation from MWRC 2011
1
star
74

hoedown2010

My presentation & code for the Ruby Hoedown 2010
1
star