Skip to content

Commit

Permalink
feat: stream support
Browse files Browse the repository at this point in the history
- closes #31
- closes #35

Signed-off-by: Lexus Drumgold <unicornware@flexdevelopment.llc>
  • Loading branch information
unicornware committed Aug 16, 2024
1 parent fae10c3 commit d30832e
Show file tree
Hide file tree
Showing 156 changed files with 3,755 additions and 2,526 deletions.
2 changes: 1 addition & 1 deletion .codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,6 @@ ignore:

profiling:
critical_files_paths:
- src/constructs/eof.ts
- src/constructs/initialize.ts
- src/lexer.ts
- src/preprocess.ts
4 changes: 4 additions & 0 deletions .dictionary.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ fbca
ggshield
gpgsign
hmarr
iife
jchen
kaisugi
lcov
Expand All @@ -22,6 +23,8 @@ mlly
nocheck
nvmrc
onreturn
onsuccessfulcheck
onsuccessfulconstruct
pathe
pkgs
preid
Expand All @@ -33,3 +36,4 @@ vates
vfile
vitest
yarnrc
zwnj
3 changes: 2 additions & 1 deletion .dprint.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
"CHANGELOG.md",
"LICENSE.md",
"RELEASE_NOTES.md",
"yarn.lock"
"yarn.lock",
"__fixtures__/markdown/*.md"
],
"exec": {
"commands": [
Expand Down
1 change: 1 addition & 0 deletions .markdownlintignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
**/CHANGELOG.md
**/LICENSE.md
**/RELEASE_NOTES.md
__fixtures__/markdown/*.md
2 changes: 0 additions & 2 deletions __fixtures__/hello.txt

This file was deleted.

1 change: 0 additions & 1 deletion __fixtures__/inline-tag.txt

This file was deleted.

31 changes: 31 additions & 0 deletions __fixtures__/markdown/code-fenced.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
```
fenced code
```

```js
fenced code with a language
```

```js line=1
fenced code with meta
```

~~~
fenced code with tildes
~~~

```not fenced code```

~~~fenced code~~~
asd
~~~

```
asd
```

```
asd
```
18 changes: 18 additions & 0 deletions __fixtures__/markdown/code-indented.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
indented code


more indented code
Not indented code

more
indent

Not code.

tabs
and mixed with spaces
extra spaces

Not code.

a tab
8 changes: 8 additions & 0 deletions __fixtures__/markdown/code-text.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
A couple of code examples: `a`, ` b`, `c `, ` d `, ` e
`, ` f
`, `g
`, `
h`.

And: `alpha bravo charlie
delta echo`.
Empty file added __fixtures__/markdown/empty.md
Empty file.
20 changes: 0 additions & 20 deletions __fixtures__/numerics.txt

This file was deleted.

3 changes: 0 additions & 3 deletions __fixtures__/strings.txt

This file was deleted.

21 changes: 0 additions & 21 deletions __fixtures__/tk.ts

This file was deleted.

16 changes: 16 additions & 0 deletions __fixtures__/tt.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* @file Fixtures - tt
* @module fixtures/tt
*/

/**
* Token types.
*
* @enum {string}
*/
enum tt {
eof = 'eof',
typeMetadata = 'typeMetadata'
}

export default tt
2 changes: 2 additions & 0 deletions __fixtures__/type-metadata.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{{ id: string }}
{string
37 changes: 37 additions & 0 deletions __tests__/constructs/code-fenced.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* @file Test Constructs - codeFenced
* @module tests/constructs/codeFenced
*/

import { codes } from '#src/enums'
import type { Construct, TokenizeContext } from '#src/interfaces'
import type { Code, Tokenizer } from '#src/types'
import * as micromark from 'micromark-core-commonmark'

/**
* Fenced code construct.
*
* @const {Construct} codeFenced
*/
const codeFenced: Construct = {
name: micromark.codeFenced.name,
test,
tokenize: micromark.codeFenced.tokenize as unknown as Tokenizer
}

export default codeFenced

/**
* Check if the current character `code` can start this construct.
*
* @see {@linkcode Code}
* @see {@linkcode TokenizeContext}
*
* @this {TokenizeContext}
*
* @param {Code} code - Current character code
* @return {boolean} `true` if `code` can start construct
*/
function test(this: TokenizeContext, code: Code): boolean {
return code === codes.graveAccent || code === codes.tilde
}
39 changes: 39 additions & 0 deletions __tests__/constructs/code-text.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* @file Test Constructs - codeText
* @module tests/constructs/codeText
*/

import { codes } from '#src/enums'
import type { Construct, TokenizeContext } from '#src/interfaces'
import type { Code, Guard, Resolver, Tokenizer } from '#src/types'
import * as micromark from 'micromark-core-commonmark'

/**
* Inline code construct.
*
* @const {Construct} codeText
*/
const codeText: Construct = {
name: micromark.codeText.name,
previous: micromark.codeText.previous as unknown as Guard,
resolve: micromark.codeText.resolve as unknown as Resolver,
test,
tokenize: micromark.codeText.tokenize as unknown as Tokenizer
}

export default codeText

/**
* Check if the current character `code` can start this construct.
*
* @see {@linkcode Code}
* @see {@linkcode TokenizeContext}
*
* @this {TokenizeContext}
*
* @param {Code} code - Current character code
* @return {boolean} `true` if `code` can start construct
*/
function test(this: TokenizeContext, code: Code): boolean {
return code === codes.graveAccent
}
90 changes: 90 additions & 0 deletions __tests__/constructs/eof.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/**
* @file Test Constructs - eof
* @module tests/constructs/eof
*/

import tt from '#fixtures/tt'
import { codes, ev } from '#src/enums'
import type { Construct, Effects, TokenizeContext } from '#src/interfaces'
import type { Code, Event, State } from '#src/types'

/**
* End of file construct.
*
* @const {Construct} eof
*/
const eof: Construct = { name: tt.eof, previous, resolveAll, tokenize }

export default eof

/**
* Check if the previous character `code` can come before this construct.
*
* @see {@linkcode Code}
* @see {@linkcode TokenizeContext}
*
* @this {TokenizeContext}
*
* @param {Code} code - Previous character code
* @return {boolean} `true` if `code` allowed before construct
*/
function previous(this: TokenizeContext, code: Code): boolean {
return typeof code === 'number'
}

/**
* Resolve all events.
*
* @see {@linkcode Event}
* @see {@linkcode TokenizeContext}
*
* @param {Event[]} events - List of events
* @param {TokenizeContext} context - Tokenize context
* @return {Event[]} Changed events
*/
function resolveAll(events: Event[], context: TokenizeContext): Event[] {
for (const [event, token] of events) {
if (event === ev.enter && token.type !== tt.eof) {
token.value = context.sliceSerialize(token)
}
}

return events
}

/**
* Set up a state machine to handle character codes streaming in.
*
* @see {@linkcode Effects}
* @see {@linkcode State}
* @see {@linkcode TokenizeContext}
*
* @this {TokenizeContext}
*
* @param {Effects} effects - Context object to transition state machine
* @param {State} ok - Successful tokenization state
* @param {State} nok - Failed tokenization state
* @return {State} Initial state
*/
function tokenize(
this: TokenizeContext,
effects: Effects,
ok: State,
nok: State
): State {
return eof

/**
* Tokenize end of file.
*
* @param {Code} code - Current character code
* @return {State | undefined} Next state
*/
function eof(code: Code): State | undefined {
if (code !== codes.eof) return nok(code)
effects.enter(tt.eof)
effects.consume(code)
effects.exit(tt.eof)
return ok
}
}
38 changes: 38 additions & 0 deletions __tests__/constructs/html-flow.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* @file Test Constructs - htmlFlow
* @module tests/constructs/htmlFlow
*/

import { codes } from '#src/enums'
import type { Construct, TokenizeContext } from '#src/interfaces'
import type { Code, Resolver, Tokenizer } from '#src/types'
import * as micromark from 'micromark-core-commonmark'

/**
* HTML flow construct.
*
* @const {Construct} htmlFlow
*/
const htmlFlow: Construct = {
name: micromark.htmlFlow.name,
resolveTo: micromark.htmlFlow.resolveTo as unknown as Resolver,
test,
tokenize: micromark.htmlFlow.tokenize as unknown as Tokenizer
}

export default htmlFlow

/**
* Check if the current character `code` can start this construct.
*
* @see {@linkcode Code}
* @see {@linkcode TokenizeContext}
*
* @this {TokenizeContext}
*
* @param {Code} code - Current character code
* @return {boolean} `true` if `code` can start construct
*/
function test(this: TokenizeContext, code: Code): boolean {
return code === codes.lt
}
Loading

0 comments on commit d30832e

Please sign in to comment.