Skip to content

Commit

Permalink
feat(parsers): PunctuatorParser
Browse files Browse the repository at this point in the history
Signed-off-by: Lexus Drumgold <unicornware@flexdevelopment.llc>
  • Loading branch information
unicornware committed Jul 3, 2024
1 parent d2fac9c commit 3b542c6
Show file tree
Hide file tree
Showing 27 changed files with 1,630 additions and 13 deletions.
4 changes: 2 additions & 2 deletions .codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -90,5 +90,5 @@ ignore:

profiling:
critical_files_paths:
- src/location.ts
- src/reader.ts
- src/constructs/*.ts
- src/parsers/*.parser.ts
6 changes: 5 additions & 1 deletion .commitlintrc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ import { scopes } from '@flex-development/commitlint-config'
const config: UserConfig = {
extends: ['@flex-development'],
rules: {
'scope-enum': [Severity.Error, 'always', scopes(['chore'])]
'scope-enum': [Severity.Error, 'always', scopes([
'chore',
'constructs',
'parsers'
])]
}
}

Expand Down
2 changes: 2 additions & 0 deletions .dictionary.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ jchen
kaisugi
kata
keyid
kmid
lcov
lintstagedrc
mdast
Expand All @@ -30,6 +31,7 @@ pathe
pkgs
preid
shfmt
succ
tokenizes
unstub
vates
Expand Down
42 changes: 42 additions & 0 deletions __tests__/setup/expect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* @file Test Setup - expect
* @module tests/setup/expect
*/

import type Token from '#src/token'
import {
ParseError,
result,
type ParserOutput,
type TokenType as TT
} from '@flex-development/unist-util-parsec'

/**
* Expect a failed parser output object.
*
* @param {ParserOutput<TT, unknown>} output - Parser output
* @return {undefined} Nothing
*/
function failed(output: ParserOutput<TT, unknown>): undefined {
void expect(output.successful).to.be.false
expect(output.error).to.be.instanceof(ParseError)
return void output
}

/**
* Expect a succeeded parser output object.
*
* @param {ParserOutput<TT, unknown>} output - Parser output
* @param {Token} token - Head token
* @return {undefined} Nothing
*/
function succeeded(output: ParserOutput<TT, unknown>, token: Token): undefined {
void expect(() => result(output)).not.to.throw
expect(output.candidate).to.have.property('head', token)
expect(output.candidate).to.have.property('next', undefined)
expect(output.candidate).to.have.property('result')
return void output
}

global.expect.failed = failed
global.expect.succeeded = succeeded
1 change: 1 addition & 0 deletions __tests__/setup/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
* @module tests/setup
*/

import './expect'
import './faker'
2 changes: 1 addition & 1 deletion __tests__/utils/inspect.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* @file Test Utilities - inspect
* @module tests/utils/isToken
* @module tests/utils/inspect
*/

import type Token from '#src/token'
Expand Down
37 changes: 37 additions & 0 deletions __tests__/utils/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* @file Test Utilities - test
* @module tests/utils/test
*/

import { tt } from '#src/enums'
import type Token from '#src/token'
import {
kmid,
tok,
type ParserOutput,
type Runner,
type TokenType
} from '@flex-development/unist-util-parsec'

/**
* Parse an occurrence of `x`, bounded by start and end of file tokens.
*
* @see {@linkcode ParserOutput}
* @see {@linkcode Runner}
* @see {@linkcode TokenType}
* @see {@linkcode Token}
*
* @template {any} R - Parse candidate result
*
* @param {Runner<TokenType, R>} x - Parser to apply
* @param {Token | undefined} token - Current token
* @return {ParserOutput<TokenType, R>} Parser output
*/
function test<R>(
x: Runner<TokenType, R>,
token: Token | undefined
): ParserOutput<TokenType, R> {
return kmid(tok(tt.sof), x, tok(tt.eof)).parse(token)
}

export default test
8 changes: 8 additions & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ export default [
'__fixtures__/underscore-1.5.2.js'
]
},
{
files: ['__tests__/setup/expect.ts'],
languageOptions: {
globals: {
expect: true
}
}
},
{
files: ['src/constructs/*.ts'],
rules: {
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,9 @@
},
"dependencies": {
"@flex-development/docast": "1.0.0-alpha.17",
"@flex-development/esast": "flex-development/esast#c27d63427454b617f92dc1f01a82f3344fca7dc4",
"@flex-development/esast": "flex-development/esast#bc319cc4d5b14e2a55767bbd60ac2bfcb9ae40b2",
"@flex-development/tutils": "6.0.0-alpha.25",
"@flex-development/unist-util-parsec": "flex-development/unist-util-parsec#commit=ce79a1bddd07cc6fbe97d20592e930eb77b5b756",
"@flex-development/unist-util-stringify-position": "1.0.0",
"@flex-development/vfile-lexer": "flex-development/vfile-lexer#commit=a4837a7d1a9c63036ed522c0e844cd7c0ed78428",
"@types/mdast": "4.0.4",
Expand Down Expand Up @@ -158,6 +159,7 @@
"yaml-eslint-parser": "1.2.3"
},
"resolutions": {
"@flex-development/esast": "flex-development/esast#bc319cc4d5b14e2a55767bbd60ac2bfcb9ae40b2",
"@flex-development/unist-util-types": "1.6.1",
"chai": "5.1.1"
},
Expand Down
4 changes: 4 additions & 0 deletions src/__tests__/index.e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ import * as testSubject from '../index'
describe('e2e:esast-util-from-code', () => {
it('should expose public api', () => {
expect(testSubject).to.have.keys([
'AbstractParser',
'Grammar',
'Lexer',
'Parser',
'PunctuatorParser',
'Token',
'errors',
'isLineEnding',
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
export * from './enums'
export type * from './interfaces'
export { default as Lexer } from './lexer'
export * from './parsers'
export { default as Token } from './token'
export type * from './types'
export * from './utils'
165 changes: 165 additions & 0 deletions src/parsers/__snapshots__/punctuator.parser.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`unit:parsers/PunctuatorParser > #arrow > should parse "=>" 1`] = `
tokens[2]
├─0 punctuator "=" (1:1-1:2, 0-1)
│ whitespace: ""
└─1 punctuator ">" (1:2-1:3, 1-2)
whitespace: ""
`;

exports[`unit:parsers/PunctuatorParser > #ellipsis > should parse "..." 1`] = `
tokens[3]
├─0 punctuator "." (1:1-1:2, 0-1)
│ whitespace: ""
├─1 punctuator "." (1:2-1:3, 1-2)
│ whitespace: ""
└─2 punctuator "." (1:3-1:4, 2-3)
whitespace: ""
`;

exports[`unit:parsers/PunctuatorParser > chars > should parse chars.ampersand 1`] = `
punctuator "&" (1:1-1:2, 0-1)
whitespace: ""
`;

exports[`unit:parsers/PunctuatorParser > chars > should parse chars.asterisk 1`] = `
punctuator "*" (1:1-1:2, 0-1)
whitespace: ""
`;

exports[`unit:parsers/PunctuatorParser > chars > should parse chars.at 1`] = `
punctuator "@" (1:1-1:2, 0-1)
whitespace: ""
`;

exports[`unit:parsers/PunctuatorParser > chars > should parse chars.backslash 1`] = `
punctuator "\\\\" (1:1-1:2, 0-1)
whitespace: ""
`;

exports[`unit:parsers/PunctuatorParser > chars > should parse chars.backtick 1`] = `
punctuator "\`" (1:1-1:2, 0-1)
whitespace: ""
`;

exports[`unit:parsers/PunctuatorParser > chars > should parse chars.bar 1`] = `
punctuator "|" (1:1-1:2, 0-1)
whitespace: ""
`;

exports[`unit:parsers/PunctuatorParser > chars > should parse chars.caret 1`] = `
punctuator "^" (1:1-1:2, 0-1)
whitespace: ""
`;

exports[`unit:parsers/PunctuatorParser > chars > should parse chars.colon 1`] = `
punctuator ":" (1:1-1:2, 0-1)
whitespace: ""
`;

exports[`unit:parsers/PunctuatorParser > chars > should parse chars.comma 1`] = `
punctuator "," (1:1-1:2, 0-1)
whitespace: ""
`;

exports[`unit:parsers/PunctuatorParser > chars > should parse chars.dollar 1`] = `
keyid "$" (1:1-1:2, 0-1)
whitespace: ""
private: false
`;

exports[`unit:parsers/PunctuatorParser > chars > should parse chars.dot 1`] = `
punctuator "." (1:1-1:2, 0-1)
whitespace: ""
`;

exports[`unit:parsers/PunctuatorParser > chars > should parse chars.equal 1`] = `
punctuator "=" (1:1-1:2, 0-1)
whitespace: ""
`;

exports[`unit:parsers/PunctuatorParser > chars > should parse chars.exclamation 1`] = `
punctuator "!" (1:1-1:2, 0-1)
whitespace: ""
`;

exports[`unit:parsers/PunctuatorParser > chars > should parse chars.gt 1`] = `
punctuator ">" (1:1-1:2, 0-1)
whitespace: ""
`;

exports[`unit:parsers/PunctuatorParser > chars > should parse chars.hash 1`] = `
punctuator "#" (1:1-1:2, 0-1)
whitespace: ""
`;

exports[`unit:parsers/PunctuatorParser > chars > should parse chars.leftBrace 1`] = `
punctuator "{" (1:1-1:2, 0-1)
whitespace: ""
`;
exports[`unit:parsers/PunctuatorParser > chars > should parse chars.leftBracket 1`] = `
punctuator "[" (1:1-1:2, 0-1)
whitespace: ""
`;
exports[`unit:parsers/PunctuatorParser > chars > should parse chars.leftParen 1`] = `
punctuator "(" (1:1-1:2, 0-1)
whitespace: ""
`;
exports[`unit:parsers/PunctuatorParser > chars > should parse chars.lt 1`] = `
punctuator "<" (1:1-1:2, 0-1)
whitespace: ""
`;
exports[`unit:parsers/PunctuatorParser > chars > should parse chars.minus 1`] = `
punctuator "-" (1:1-1:2, 0-1)
whitespace: ""
`;
exports[`unit:parsers/PunctuatorParser > chars > should parse chars.percent 1`] = `
punctuator "%" (1:1-1:2, 0-1)
whitespace: ""
`;
exports[`unit:parsers/PunctuatorParser > chars > should parse chars.plus 1`] = `
punctuator "+" (1:1-1:2, 0-1)
whitespace: ""
`;
exports[`unit:parsers/PunctuatorParser > chars > should parse chars.question 1`] = `
punctuator "?" (1:1-1:2, 0-1)
whitespace: ""
`;
exports[`unit:parsers/PunctuatorParser > chars > should parse chars.rightBrace 1`] = `
punctuator "}" (1:1-1:2, 0-1)
whitespace: ""
`;
exports[`unit:parsers/PunctuatorParser > chars > should parse chars.rightBracket 1`] = `
punctuator "]" (1:1-1:2, 0-1)
whitespace: ""
`;
exports[`unit:parsers/PunctuatorParser > chars > should parse chars.rightParen 1`] = `
punctuator ")" (1:1-1:2, 0-1)
whitespace: ""
`;
exports[`unit:parsers/PunctuatorParser > chars > should parse chars.semicolon 1`] = `
punctuator ";" (1:1-1:2, 0-1)
whitespace: ""
`;
exports[`unit:parsers/PunctuatorParser > chars > should parse chars.slash 1`] = `
punctuator "/" (1:1-1:2, 0-1)
whitespace: ""
`;
exports[`unit:parsers/PunctuatorParser > chars > should parse chars.tilde 1`] = `
punctuator "~" (1:1-1:2, 0-1)
whitespace: ""
`;
Loading

0 comments on commit 3b542c6

Please sign in to comment.