Skip to content

Commit

Permalink
feat(tsdoc): add tsdocs for parsers
Browse files Browse the repository at this point in the history
  • Loading branch information
norskeld committed Jul 12, 2022
1 parent 3393f1e commit c78d57a
Show file tree
Hide file tree
Showing 16 changed files with 154 additions and 7 deletions.
6 changes: 1 addition & 5 deletions .releaserc.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@
"@semantic-release/commit-analyzer",
{
"preset": "angular",
"releaseRules": [
{ "type": "docs", "scope": "readme", "release": "patch" },
{ "type": "docs", "scope": "tsdoc", "release": "patch" },
{ "scope": "no-release", "release": false }
]
"releaseRules": [{ "scope": "no-release", "release": false }]
}
],
"@semantic-release/release-notes-generator",
Expand Down
4 changes: 2 additions & 2 deletions docs/content/parsers/oneOf.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
title: 'oneOf'
kind: 'primitive'
description: 'oneOf tries to match one of the characters in the given string.'
description: 'oneOf ensures that one of the characters in the given string matches the current character.'
---

```typescript {{ withLineNumbers: false }}
Expand All @@ -10,7 +10,7 @@ function oneOf(): Parser<string>

## Description

`oneOf` tries to match one of the characters in the given string.
`oneOf` ensures that one of the characters in the given string matches the current character.

## Usage

Expand Down
5 changes: 5 additions & 0 deletions src/parsers/any.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { type Parser } from '../state'

/**
* Parses any single character from the input and returns it. Fails at the end of input.
*
* @returns A single parsed character.
*/
export function any(): Parser<string> {
return {
parse(input, pos) {
Expand Down
52 changes: 52 additions & 0 deletions src/parsers/defer.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,61 @@
import { type Parser } from '../state'

/**
* Intersection type to add a method for deferred parser definition.
*
* @internal
*/
type Deferred<T> = Parser<T> & {
with(parser: Parser<T>): void
}

/**
* This is a special parser that has an additional `with` method, which should be used to define the
* parser. This parser is tailored for creating mutually recursive parsers.
*
* @example
*
* ```typescript
* interface NumberNode {
* type: 'number'
* value: number
* }
*
* interface ListNode {
* type: 'list'
* value: Array<NumberNode | ListNode>
* }
*
* // Here we create 'dummies'
*
* const TupleList = defer<ListNode>()
* const TupleNumber = defer<NumberNode>()
*
* // And below we actually define parsers
*
* TupleNumber.with(
* map(
* int(),
* (value) => ({ type: 'number', value })
* )
* )
*
* TupleList.with(
* map(
* takeMid(
* string('('),
* sepBy(choice(TupleList, TupleNumber), string(',')),
* string(')')
* ),
* (value) => ({ type: 'list', value })
* )
* )
*
* console.log(
* run(TupleList).with('(1,2,(3,(4,5)))')
* )
* ```
*/
export function defer<T>(): Deferred<T> {
let deferred: Parser<T> | null = null

Expand Down
5 changes: 5 additions & 0 deletions src/parsers/eof.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { type Parser } from '../state'

/**
* Only succeeds at the end of the input.
*
* @returns `null`
*/
export function eof(): Parser<null> {
return {
parse(input, pos) {
Expand Down
5 changes: 5 additions & 0 deletions src/parsers/eol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ import { string } from './string'
const EOL_UNIX = '\n'
const EOL_NON_UNIX = '\r\n'

/**
* Only succeeds at the end of the line, either `\n` or `\r\n`.
*
* @returns Matched line break character
*/
export function eol(): Parser<string> {
return error(choice(string(EOL_UNIX), string(EOL_NON_UNIX)), 'end of line')
}
5 changes: 5 additions & 0 deletions src/parsers/float.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ import { regexp } from './regexp'

const FLOAT_RE = /-?\d+\.\d+/g

/**
* Parses a floating point number.
*
* @returns Parsed float as a number
*/
export function float(): Parser<number> {
return {
parse(input, pos) {
Expand Down
10 changes: 10 additions & 0 deletions src/parsers/letter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,20 @@ import { regexp } from './regexp'
const LETTER_RE = /\p{Letter}/gu
const LETTERS_RE = /\p{Letter}+/gu

/**
* Parses a single alphabetical character. Unicode friendly.
*
* @returns Matched character.
*/
export function letter(): Parser<string> {
return regexp(LETTER_RE, 'letter')
}

/**
* Parses a sequence of alphabetical characters. Unicode friendly.
*
* @returns Matched characters as a string.
*/
export function letters(): Parser<string> {
return regexp(LETTERS_RE, 'letters')
}
7 changes: 7 additions & 0 deletions src/parsers/noneOf.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import { type Parser } from '../state'

/**
* Ensures that none of the characters in the given string matches the current character.
*
* @param chars - A string of characters that current character should not match
*
* @returns Current character
*/
export function noneOf(chars: string): Parser<string> {
const charset = [...chars]

Expand Down
5 changes: 5 additions & 0 deletions src/parsers/nothing.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { type Parser } from '../state'

/**
* Simply resolves to `null`.
*
* @returns `null`.
*/
export function nothing(): Parser<null> {
return {
parse(_, pos) {
Expand Down
7 changes: 7 additions & 0 deletions src/parsers/oneOf.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import { type Parser } from '../state'

/**
* Ensures that one of the characters in the given string matches the current character.
*
* @param chars - A string of characters that current character should match
*
* @returns Current character
*/
export function oneOf(chars: string): Parser<string> {
const charset = [...chars]

Expand Down
15 changes: 15 additions & 0 deletions src/parsers/regexp.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
import { type Parser } from '../state'

/**
* Parses a string that matches a provided `re` regular expression. Returns the matched string, or
* fails with an `expected` message.
*
* The regular expression must obey three simple rules:
*
* - It *does* use `g` flag. Flags like `u` and `i` are allowed and can be added if needed.
* - It *doesn't* use `^` and `$` to match at the beginning or at the end of the text.
* - It *doesn't* use capturing groups.
*
* @param re - Regular expression
* @param expected - Error message if the regular expression does not match input
*
* @returns Matched string
*/
export function regexp(re: RegExp, expected: string): Parser<string> {
return {
parse(input, pos) {
Expand Down
5 changes: 5 additions & 0 deletions src/parsers/rest.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { type Parser } from '../state'

/**
* Simply returns the unparsed input as a string. Never fails.
*
* @returns Rest of the input as a string
*/
export function rest(): Parser<string> {
return {
parse(input, pos) {
Expand Down
11 changes: 11 additions & 0 deletions src/parsers/run.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
import type { Parser, Result } from '../state'

/**
* @internal
*/
interface Runnable<T> {
with(input: string): Result<T>
}

/**
* Runs a parser with provided input.
*
* @typeParam T - Type of parser's result
* @param parser - Parser to run
*
* @returns Parser result
*/
export function run<T>(parser: Parser<T>): Runnable<T> {
return {
with(input) {
Expand Down
14 changes: 14 additions & 0 deletions src/parsers/string.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import { type Parser } from '../state'
import { size } from '../utils/unicode'

/**
* Parses an *ASCII* string. For parsing Unicode strings, consider using `ustring`.
*
* @param match - String to parse
*
* @returns Parsed string
*/
export function string(match: string): Parser<string> {
return {
parse(input, pos) {
Expand Down Expand Up @@ -28,6 +35,13 @@ export function string(match: string): Parser<string> {
}
}

/**
* Parses a Unicode string. For parsing ASCII-only strings, consider using `string`.
*
* @param match - String to parse
*
* @returns Parsed string
*/
export function ustring(match: string): Parser<string> {
return {
parse(input, pos) {
Expand Down
5 changes: 5 additions & 0 deletions src/parsers/whitespace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ import { regexp } from './regexp'

const WHITESPACE_REQUIRED_RE = /\s+/g

/**
* Parses whitespace, either a single character or consecutive ones.
*
* @returns Matched whitespace character(s)
*/
export function whitespace(): Parser<string> {
return regexp(WHITESPACE_REQUIRED_RE, 'whitespace')
}

0 comments on commit c78d57a

Please sign in to comment.