https://github.com/oelin/april
Probably the smallest recursive descent parser in existence 🤏.
https://github.com/oelin/april
javascript parser recursive-descent-parser tiny
Last synced: about 2 months ago
JSON representation
Probably the smallest recursive descent parser in existence 🤏.
- Host: GitHub
- URL: https://github.com/oelin/april
- Owner: oelin
- License: mit
- Created: 2021-04-12T12:13:28.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2023-01-17T12:11:13.000Z (over 2 years ago)
- Last Synced: 2024-09-30T09:04:05.606Z (7 months ago)
- Topics: javascript, parser, recursive-descent-parser, tiny
- Language: JavaScript
- Homepage:
- Size: 68.4 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: LICENSE
Awesome Lists containing this project
README
# April 🌺
Probably the smallest recursive descent parser in existence 🤏.
```js
const { match, peek, feed } = require('april')function letters() {
return match(/^\w+/)
}function separator() {
return match(/^\W*/) // stuff like spaces and periods
}function word() {
sparator()
return letters()
}
```Let's test it out!
```js
feed('Luke, I am your father')word() // returns 'Luke'
word() // returns 'I'"
word() // returns 'am'
```### Use peek to check the next token
```js
function upper() {
return match(/^[A-Z]/)
}function lower() {
return match(/^[a-z]/)
}feed("AbCdE")
peek(upper) // returns `true` because the next token is uppercase
peek(lower) // returns `false` because the next token isn't lowercase
```### Use skip to add choices
```js
const { skip } = require('aprils')// match lowercase or uppercase
function letter() {
return skip(lower) || upper()
}feed('AbC')
letter() // returns 'A'
letter() // returns 'b'
letter() // returns 'C'
```You can add more choices using JavaScript's `||` operator
```js
// accept A or B or ... or Zskip(A) || skip(B) || ... || skip(Y) || Z()
```## API
### feed(string)
Sets the input string.
### match(pattern)
Consumes and returns part of the input string that matches `pattern`. Note that `pattern` should start with `^` to match from the start of the input string.
### peek(parser, [args...])
Executes a parser and returns true if it was successful. The input string isn't consumed.
### skip(parser, [args...])
Executes a parser and returns the result if it was successful. The input string is only consumed on success.