Check-in [7f2e588e5f]

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Preparing to publish as an NPM package.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | trunk
Files: files | file ages | folders
SHA1:7f2e588e5f9bf3577af243a65c9f3c10f1b34086
User & Date: User 2017-11-29 02:24:33
Context
2017-11-29
02:24
Preparing to publish as an NPM package. Leaf check-in: 7f2e588e5f user: User tags: trunk
2016-11-13
16:52
[55b0352193] Created a parser named space that optionally consumes any whitespace, and inserted into the formula parser where needed. check-in: acbb9469fc user: adamc tags: trunk
Changes

Added README.md.



















































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# PComb
## A parser combinator library written in TypeScript
A collection of general and specific-purpose parsers that can be composed with combinator functions to very powerful effect.
### Parsers
A parser is a function that takes a **ParserInput**, a **ParserOutput** and returns a **ParseResult**.
#### ParseResult
A **ParseResult** is a three-element tuple with these values:

1. a **boolean** that indicates if the parser successfully matched the input 

2. a copy of the **ParserInput** object that was passed in with any matched test removed from the text property

3. a copy of the **ParserOutput** object that was passed in with any matched text appended to the matched array

#### ParserInput
The code that uses PComb must provide input to the library with a class that implements the **ParserInput** interface:
```
export interface ParserInput {
     /** What is the text that is being parsed? */
     text: string;
     copy(): ParserInput;
 }
 ```

 #### ParserOutput
 Similarly to **ParserInput**, a class that implements **ParserOutput** is provided by the calling code to record the units of text that have been matched by individual parser as well to record any state that is specific to the particular use:
 ```
 export interface ParserOutput {
     /** The text that was matched by the parser. */
     matched: Array<string>;
     copy(): ParserOutput;
 }
 ```

### Combinators
A combinator is a higher-order function that takes one or more **Parser** functions as input and produces a new function that wraps and executes them in some particular way.
+ or -- matches one of a list of parsers. After one Parser matches the input, the or exits with a success result.
+ and -- match all of the listed parsers in order. If one fails to match, the and exits with a failure result.
+ opt -- optionally match a parser; always produces a success result.
+ all -- match the given single parser until matching fails; return all matches as a single match. If the given Parser does not match at least once, returns a failure result.
+ any -- collect all input as a single match up to the point that the provided parser matches. Always successful.
+ exact -- require that the given parser consume all available input; otherwise, parsing fails.
+ apply -- execute the given ParserAction if the given parser succeeds.

### ParserAction
A **ParserAction** is a function that takes a string of matched characters and a **ParserOutput** and returns a **ParserOutput**:
```
export type ParserAction = (matchedText: string, output: ParserOutput) => ParserOutput;
```
If such a function is provided to a Parser, it will be executed on a successful match. The returned **ParserOutput** object then becomes the return value for the Parser. This mechanism allows calling code to have use-specific state that is recorded and mutated on parser matching.

### Using PComb
There's pretty decent documentation available for PComb. Take a look at the [Getting Started](https://chiselapp.com/user/acrossland/repository/pcomb/wiki?name=Getting+Started) page and the [API Reference](https://chiselapp.com/user/acrossland/repository/pcomb/wiki?name=API+Reference).

Also, the source repo has extensive automated tests and a somewhat complex example application to look at to see how it works in practice.
### The Code
The [source code](https://chiselapp.com/user/acrossland/repository/pcomb/home) for PComb is freely available.

Changes to gulpfile.js.

8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
  gulp.src('src/*.ts')
    .pipe(sourcemaps.init())
    .pipe(ts({
      declarationFiles: true,
      noImplicitAny: true
    }))
    .pipe(sourcemaps.write('.'))
    .pipe(gulp.dest('src/'))
    .on('end', function() { done(); });
});

gulp.task('demo', function () {
  gulp.src('app/*.ts')
    .pipe(ts({
      declarationFiles: true,







|







8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
  gulp.src('src/*.ts')
    .pipe(sourcemaps.init())
    .pipe(ts({
      declarationFiles: true,
      noImplicitAny: true
    }))
    .pipe(sourcemaps.write('.'))
    .pipe(gulp.dest('dist/src'))
    .on('end', function() { done(); });
});

gulp.task('demo', function () {
  gulp.src('app/*.ts')
    .pipe(ts({
      declarationFiles: true,

Changes to package.json.

1
2
3
4
5

6
7
8
9
10
11
12
..
18
19
20
21
22
23
24
25
26
27
{
  "name": "pcomb",
  "version": "1.0.0",
  "description": "A Parser combinator library written in TypeScript.",
  "main": "gulpfile.js",

  "directories": {
    "test": "tests"
  },
  "dependencies": {
    "tsunit.external": "^2.0.10"
  },
  "devDependencies": {
................................................................................
    "gulp-typescript": "^3.0.2",
    "typescript": "^2.0.6",
    "vinyl-source-stream": "^1.1.0"
  },
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC"
}



|
|
>







 







|


1
2
3
4
5
6
7
8
9
10
11
12
13
..
19
20
21
22
23
24
25
26
27
28
{
  "name": "pcomb",
  "version": "1.0.0",
  "description": "A Parser combinator library.",
  "main": "./dist/src/pcomb.js",
  "types": "./dist/src/pcomb.d.ts",
  "directories": {
    "test": "tests"
  },
  "dependencies": {
    "tsunit.external": "^2.0.10"
  },
  "devDependencies": {
................................................................................
    "gulp-typescript": "^3.0.2",
    "typescript": "^2.0.6",
    "vinyl-source-stream": "^1.1.0"
  },
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "Adam Crossland",
  "license": "ISC"
}