Add syntax highlighting to code blocks in README.md files

redux
David Majda 8 years ago
parent 45de51a881
commit 138405d89d

@ -37,11 +37,15 @@ Installation
To use the `pegjs` command, install PEG.js globally: To use the `pegjs` command, install PEG.js globally:
```console
$ npm install -g pegjs $ npm install -g pegjs
```
To use the JavaScript API, install PEG.js locally: To use the JavaScript API, install PEG.js locally:
```console
$ npm install pegjs $ npm install pegjs
```
If you need both the `pegjs` command and the JavaScript API, install PEG.js both If you need both the `pegjs` command and the JavaScript API, install PEG.js both
ways. ways.
@ -51,7 +55,9 @@ ways.
[Download](http://pegjs.org/#download) the PEG.js library (regular or minified [Download](http://pegjs.org/#download) the PEG.js library (regular or minified
version) or install it using Bower: version) or install it using Bower:
```console
$ bower install pegjs $ bower install pegjs
```
Generating a Parser Generating a Parser
------------------- -------------------
@ -64,12 +70,16 @@ input). Generated parser itself is a JavaScript object with a simple API.
To generate a parser from your grammar, use the `pegjs` command: To generate a parser from your grammar, use the `pegjs` command:
```console
$ pegjs arithmetics.pegjs $ pegjs arithmetics.pegjs
```
This writes parser source code into a file with the same name as the grammar This writes parser source code into a file with the same name as the grammar
file but with “.js” extension. You can also specify the output file explicitly: file but with “.js” extension. You can also specify the output file explicitly:
```console
$ pegjs arithmetics.pegjs arithmetics-parser.js $ pegjs arithmetics.pegjs arithmetics-parser.js
```
If you omit both input and output file, standard input and output are used. If you omit both input and output file, standard input and output are used.
@ -96,7 +106,9 @@ You can tweak the generated parser with several options:
In Node.js, require the PEG.js parser generator module: In Node.js, require the PEG.js parser generator module:
```javascript
var peg = require("pegjs"); var peg = require("pegjs");
```
In browser, include the PEG.js library in your web page or application using the In browser, include the PEG.js library in your web page or application using the
`<script>` tag. If PEG.js detects an AMD loader, it will define itself as a `<script>` tag. If PEG.js detects an AMD loader, it will define itself as a
@ -105,7 +117,9 @@ module, otherwise the API will be available in the `peg` global object.
To generate a parser, call the `peg.generate` method and pass your grammar as a To generate a parser, call the `peg.generate` method and pass your grammar as a
parameter: parameter:
```javascript
var parser = peg.generate("start = ('a' / 'b')+"); var parser = peg.generate("start = ('a' / 'b')+");
```
The method will return generated parser object or its source code as a string The method will return generated parser object or its source code as a string
(depending on the value of the `output` option — see below). It will throw an (depending on the value of the `output` option — see below). It will throw an
@ -144,9 +158,11 @@ value depends on the grammar used to generate the parser) or throw an exception
if the input is invalid. The exception will contain `location`, `expected` and if the input is invalid. The exception will contain `location`, `expected` and
`message` properties with more details about the error. `message` properties with more details about the error.
```javascript
parser.parse("abba"); // returns ["a", "b", "b", "a"] parser.parse("abba"); // returns ["a", "b", "b", "a"]
parser.parse("abcd"); // throws an exception parser.parse("abcd"); // throws an exception
```
You can tweak parser behavior by passing a second parameter with an options You can tweak parser behavior by passing a second parameter with an options
object to the `parse` method. The following options are supported: object to the `parse` method. The following options are supported:
@ -166,6 +182,7 @@ ignores whitespace between tokens. You can also use JavaScript-style comments
Let's look at example grammar that recognizes simple arithmetic expressions like Let's look at example grammar that recognizes simple arithmetic expressions like
`2*(3+4)`. A parser generated from this grammar computes their values. `2*(3+4)`. A parser generated from this grammar computes their values.
```pegjs
start start
= additive = additive
@ -183,6 +200,7 @@ Let's look at example grammar that recognizes simple arithmetic expressions like
integer "integer" integer "integer"
= digits:[0-9]+ { return parseInt(digits.join(""), 10); } = digits:[0-9]+ { return parseInt(digits.join(""), 10); }
```
On the top level, the grammar consists of *rules* (in our example, there are On the top level, the grammar consists of *rules* (in our example, there are
five of them). Each rule has a *name* (e.g. `integer`) that identifies the rule, five of them). Each rule has a *name* (e.g. `integer`) that identifies the rule,
@ -209,6 +227,7 @@ passed to the parser using the `options` variable. Curly braces in the
initializer code must be balanced. Let's look at the example grammar from above initializer code must be balanced. Let's look at the example grammar from above
using a simple initializer. using a simple initializer.
```pegjs
{ {
function makeInteger(o) { function makeInteger(o) {
return parseInt(o.join(""), 10); return parseInt(o.join(""), 10);
@ -232,6 +251,7 @@ using a simple initializer.
integer "integer" integer "integer"
= digits:[0-9]+ { return makeInteger(digits); } = digits:[0-9]+ { return makeInteger(digits); }
```
The parsing expressions of the rules are used to match the input text to the The parsing expressions of the rules are used to match the input text to the
grammar. There are various types of expressions — matching characters or grammar. There are various types of expressions — matching characters or
@ -340,10 +360,12 @@ the initializer at the beginning of the grammar.
The code inside the predicate can also access location information using the The code inside the predicate can also access location information using the
`location` function. It returns an object like this: `location` function. It returns an object like this:
```javascript
{ {
start: { offset: 23, line: 5, column: 6 }, start: { offset: 23, line: 5, column: 6 },
end: { offset: 23, line: 5, column: 6 } end: { offset: 23, line: 5, column: 6 }
} }
```
The `start` and `end` properties both refer to the current parse position. The The `start` and `end` properties both refer to the current parse position. The
`offset` property contains an offset as a zero-based index and `line` and `offset` property contains an offset as a zero-based index and `line` and
@ -369,10 +391,12 @@ the initializer at the beginning of the grammar.
The code inside the predicate can also access location information using the The code inside the predicate can also access location information using the
`location` function. It returns an object like this: `location` function. It returns an object like this:
```javascript
{ {
start: { offset: 23, line: 5, column: 6 }, start: { offset: 23, line: 5, column: 6 },
end: { offset: 23, line: 5, column: 6 } end: { offset: 23, line: 5, column: 6 }
} }
```
The `start` and `end` properties both refer to the current parse position. The The `start` and `end` properties both refer to the current parse position. The
`offset` property contains an offset as a zero-based index and `line` and `offset` property contains an offset as a zero-based index and `line` and
@ -434,10 +458,12 @@ using the `text` function.
The code inside the action can also access location information using the The code inside the action can also access location information using the
`location` function. It returns an object like this: `location` function. It returns an object like this:
```javascript
{ {
start: { offset: 23, line: 5, column: 6 }, start: { offset: 23, line: 5, column: 6 },
end: { offset: 25, line: 5, column: 8 } end: { offset: 25, line: 5, column: 8 }
} }
```
The `start` property refers to the position at the beginning of the expression, The `start` property refers to the position at the beginning of the expression,
the `end` property refers to position after the end of the expression. The the `end` property refers to position after the end of the expression. The

@ -13,11 +13,15 @@ All commands in the following steps need to be executed in PEG.js root directory
1. Install all PEG.js dependencies, including development ones: 1. Install all PEG.js dependencies, including development ones:
```console
$ npm install $ npm install
```
2. Execute the benchmark suite: 2. Execute the benchmark suite:
```console
$ make spec $ make spec
```
3. Wait for results. 3. Wait for results.
@ -31,15 +35,21 @@ All commands in the following steps need to be executed in PEG.js root directory
2. Install all PEG.js dependencies, including development ones: 2. Install all PEG.js dependencies, including development ones:
```console
$ npm install $ npm install
```
3. Build browser version of PEG.js: 3. Build browser version of PEG.js:
```console
$ make browser $ make browser
```
4. Serve PEG.js root directory using a web server: 4. Serve PEG.js root directory using a web server:
```console
$ python -m SimpleHTTPServer $ python -m SimpleHTTPServer
```
5. Point your browser to the [benchmark suite](http://localhost:8000/benchmark/index.html). 5. Point your browser to the [benchmark suite](http://localhost:8000/benchmark/index.html).

@ -12,11 +12,15 @@ All commands in the following steps need to be executed in PEG.js root directory
1. Install all PEG.js dependencies, including development ones: 1. Install all PEG.js dependencies, including development ones:
```console
$ npm install $ npm install
```
2. Execute the spec suite: 2. Execute the spec suite:
```console
$ make spec $ make spec
```
3. Watch the specs pass (or fail). 3. Watch the specs pass (or fail).
@ -30,15 +34,21 @@ All commands in the following steps need to be executed in PEG.js root directory
2. Install all PEG.js dependencies, including development ones: 2. Install all PEG.js dependencies, including development ones:
```console
$ npm install $ npm install
```
3. Build browser version of PEG.js: 3. Build browser version of PEG.js:
```console
$ make browser $ make browser
```
4. Serve PEG.js root directory using a web server: 4. Serve PEG.js root directory using a web server:
```console
$ python -m SimpleHTTPServer $ python -m SimpleHTTPServer
```
5. Point your browser to the [spec suite](http://localhost:8000/spec/index.html). 5. Point your browser to the [spec suite](http://localhost:8000/spec/index.html).

Loading…
Cancel
Save