Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: include paren tokens around scope #37

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 47 additions & 11 deletions lib/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,29 @@ function summary (scanner) {
node.children.push(t)
}

// ... "(" <scope> ")" ...
let s = scope(scanner)
if (s instanceof Error) {
// ... <scopeStart> <scope> <scopeEnd> ...
let ss = scopeStart(scanner)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would be tempted to pull scope-start an scope-end into the scope parser, replacing the current logic that just ignores ( and ).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought the same thing, and did so at first, the issue was I didn't see an example of returning multiple tokens. I can have it return an array or error, but that would be a new api in here. Is that the direction we want to go?

if (ss instanceof Error) {
s = null
} else {
node.children.push(s)
node.children.push(ss)

let s = scope(scanner)
if (s instanceof Error) {
s = null
} else {
node.children.push(s)

let se = scopeEnd(scanner)
if (se instanceof Error) {
s = null
} else {
node.children.push(se)
}
}
}


// ... ["!"] ...
let b = breakingChange(scanner)
if (b instanceof Error) {
Expand Down Expand Up @@ -154,15 +169,23 @@ function text (scanner) {
}

/*
* "(" <scope> ")" ::= 1*<any UTF8-octets except newline or parens>
* <scopeStart> ::= "("
*/
function scope (scanner) {
if (scanner.peek() !== '(') {
return scanner.abort(scanner.enter('scope', ''))
} else {
scanner.next()
function scopeStart (scanner) {
const node = scanner.enter('scopeStart', '')
// '('
if (scanner.peek() === '(') {
node.value = scanner.next()
return scanner.exit(node)
}

return scanner.abort(node)
}

/*
* "(" <scope> ")" ::= 1*<any UTF8-octets except newline or parens>
*/
function scope (scanner) {
const node = scanner.enter('scope', '')

while (!scanner.eof()) {
Expand All @@ -177,7 +200,6 @@ function scope (scanner) {
throw scanner.abort(node, [')'])
} else {
scanner.exit(node)
scanner.next()
}

if (node.value === '') {
Expand All @@ -187,6 +209,20 @@ function scope (scanner) {
}
}

/*
* <scopeEnd> ::= ")"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to match the rest of the grammar, I'd probably make it <scope-start>/<scope-end>.

*/
function scopeEnd (scanner) {
const node = scanner.enter('scopeEnd', '')
// ')'
if (scanner.peek() === ')') {
node.value = scanner.next()
return scanner.exit(node)
}

return scanner.abort(node)
}

/*
* <body> ::= [<any body-text except pre-footer>], <newline>, <body>*
* | [<any body-text except pre-footer>]
Expand Down