Skip to content
Closed
Show file tree
Hide file tree
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
22 changes: 21 additions & 1 deletion src/parse/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Ast as AST, Parser } from 'apg-lite';
import { Ast as AST, Parser, identifiers, utilities } from 'apg-lite';

import Grammar from '../path-templating.js';
import slashCallback from './callbacks/slash.js';
Expand All @@ -9,6 +9,24 @@ import templateExpressionParamNameCallback from './callbacks/template-expression

const grammar = new Grammar();

const questionMark = (state, chars, phraseIndex, phraseLength, data) => {
if (state === identifiers.SEM_PRE) {
data.push(['question-mark', utilities.charsToString(chars, phraseIndex, phraseLength)]);
} else if (state === identifiers.SEM_POST) {
/* not used in this example */
}
return identifiers.SEM_OK;
};

const queryString = (state, chars, phraseIndex, phraseLength, data) => {
if (state === identifiers.SEM_PRE) {
data.push(['query-string', utilities.charsToString(chars, phraseIndex, phraseLength)]);
} else if (state === identifiers.SEM_POST) {
/* not used in this example */
}
return identifiers.SEM_OK;
};

const parse = (pathTemplate) => {
const parser = new Parser();

Expand All @@ -18,6 +36,8 @@ const parse = (pathTemplate) => {
parser.ast.callbacks['path-literal'] = pathLiteralCallback;
parser.ast.callbacks['template-expression'] = templateExpressionCallback;
parser.ast.callbacks['template-expression-param-name'] = templateExpressionParamNameCallback;
parser.ast.callbacks['question-mark'] = questionMark;
parser.ast.callbacks['query-string'] = queryString;

const result = parser.parse(grammar, 'path-template', pathTemplate);

Expand Down
40 changes: 21 additions & 19 deletions src/path-templating.bnf
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
; OpenAPI Path Templating ABNF syntax
; variant of https://datatracker.ietf.org/doc/html/rfc3986#section-3.3
path-template = slash *( path-segment slash ) [ path-segment ]
path-segment = 1*( path-literal / template-expression )
slash = "/"
path-literal = 1*pchar
template-expression = "{" template-expression-param-name "}"
template-expression-param-name = 1*( %x00-7A / %x7C / %x7E-10FFFF ) ; every UTF8 character except { and } (from OpenAPI)

; https://datatracker.ietf.org/doc/html/rfc3986#section-3.3
pchar = unreserved / pct-encoded / sub-delims / ":" / "@"
unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"
; https://datatracker.ietf.org/doc/html/rfc3986#section-2.3
pct-encoded = "%" HEXDIG HEXDIG
; https://datatracker.ietf.org/doc/html/rfc3986#section-2.1
sub-delims = "!" / "$" / "&" / "'" / "(" / ")"
/ "*" / "+" / "," / ";" / "="
; https://datatracker.ietf.org/doc/html/rfc3986#section-2.2
path-template = slash *( path-segment slash ) [ path-segment ] [ question-mark query-string ]
path-segment = 1*( path-literal / template-expression )
path-literal = 1*pchar
template-expression = "{" template-expression-param-name "}"
template-expression-param-name = 1*( %x00-7A / %x7C / %x7E-10FFFF )

; https://datatracker.ietf.org/doc/html/rfc5234#appendix-B.1
ALPHA = %x41-5A / %x61-7A ; A-Z / a-z
DIGIT = %x30-39 ; 0-9
HEXDIG = DIGIT / "A" / "B" / "C" / "D" / "E" / "F"
query-string = query-param *( "&" query-param )
query-param = key "=" value
key = 1*( ALPHA / DIGIT / "-" / "." / "_" / "~" )
value = *( ALPHA / DIGIT / "-" / "." / "_" / "~" / "%" / "!" / "*" / "'" / "(" / ")" / "," )

slash = "/"
question-mark = "?"

pchar = unreserved / pct-encoded / sub-delims / ":" / "@"
unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"
pct-encoded = "%" HEXDIG HEXDIG
sub-delims = "!" / "$" / "&" / "'" / "(" / ")" / "*" / "+" / "," / ";" / "="

ALPHA = %x41-5A / %x61-7A
DIGIT = %x30-39
HEXDIG = DIGIT / "A" / "B" / "C" / "D" / "E" / "F"
Loading