Skip to content

Commit

Permalink
Merge d6c62a3 into 047b903
Browse files Browse the repository at this point in the history
  • Loading branch information
simonseyock committed Apr 19, 2022
2 parents 047b903 + d6c62a3 commit 8118169
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 50 deletions.
2 changes: 1 addition & 1 deletion src/parslets/ObjectParslet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export function createObjectParslet ({ objectFieldGrammar, allowKeyTypes }: {
} else {
break
}
const type = parser.getLexer().token().type
const type = parser.lexer.current.type
if (type === '}') {
break
}
Expand Down
5 changes: 3 additions & 2 deletions test/FunctionParslet.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { expect } from 'chai'
import { jsdocGrammar } from '../src/grammars/jsdocGrammar'
import { createFunctionParslet } from '../src/parslets/FunctionParslet'
import { Parser } from '../src/Parser'
import { Lexer } from '../src/lexer/Lexer'

function parse (text: string): void {
// Replace other function parslet with one setting
Expand All @@ -14,8 +15,8 @@ function parse (text: string): void {
allowNoReturnType: true
})
]
const parser = new Parser({ grammar })
parser.parseText(text)
const parser = new Parser(grammar, Lexer.create(text))
parser.parse()
}

describe('`createFunctionParslet`', () => {
Expand Down
12 changes: 7 additions & 5 deletions test/ObjectParslet.api.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { Grammar } from '../src/grammars/Grammar'

import { createObjectParslet } from '../src/parslets/ObjectParslet'
import { RootResult } from '../src/result/RootResult'
import { Lexer } from '../src/lexer/Lexer'

describe('`ObjectParslet`', () => {
beforeEach(() => {
Expand All @@ -26,12 +27,13 @@ describe('`ObjectParslet`', () => {
objectFieldGrammar
})

const parser = new Parser({
grammar: [
const parser = new Parser(
[
objectParslet,
...jsdocGrammar
]
})
],
Lexer.create('{abc}')
)

const ret: unknown = undefined

Expand All @@ -41,7 +43,7 @@ describe('`ObjectParslet`', () => {
ret as RootResult
)

const rootResult = parser.parseText('{abc}')
const rootResult = parser.parse()

expect(rootResult).to.deep.equal({
elements: [
Expand Down
37 changes: 37 additions & 0 deletions test/ParameterListParslet.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { createFunctionParslet } from '../src/parslets/FunctionParslet'
import { Lexer } from '../src/lexer/Lexer'
import { expect } from 'chai'
import { Parser } from '../src/Parser'
import { createParameterListParslet } from '../src/parslets/ParameterListParslet'
import { createNameParslet } from '../src/parslets/NameParslet'
import { createObjectParslet } from '../src/parslets/ObjectParslet'
import { parenthesisParslet } from '../src/parslets/ParenthesisParslet'

describe('ParameterListParslet', () => {
it('rethrows errors', () => {
const parser = new Parser(
[
createFunctionParslet({
allowWithoutParenthesis: false,
allowNamedParameters: undefined,
allowNoReturnType: true
}),
createParameterListParslet({
allowTrailingComma: false
}),
createNameParslet({
allowedAdditionalTokens: []
}),
createObjectParslet({
allowKeyTypes: false,
objectFieldGrammar: []
}),
parenthesisParslet
],
Lexer.create('function(a, b, ")')
)
expect(() => {
parser.parse()
}).to.throw('Unterminated String')
})
})
19 changes: 7 additions & 12 deletions test/Parser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ import { Parser } from '../src/Parser'
import { NoParsletFoundError, EarlyEndOfParseError } from '../src/errors'

import { typescriptGrammar } from '../src/grammars/typescriptGrammar'
import { Lexer } from '../src/lexer/Lexer'

describe('Parser', () => {
it('should consume an array of tokens', () => {
const parser = new Parser({
grammar: typescriptGrammar
})
parser.parseText('[test]')
const parser = new Parser(typescriptGrammar, Lexer.create('[test]'))
parser.parse()

const twoTokens = parser.consume(['[', 'Identifier'])
const finalTokens = parser.consume([']', 'EOF'])
Expand All @@ -20,13 +19,11 @@ describe('Parser', () => {
})

it('should return token of error with `NoParsletFoundError.getToken`', () => {
const parser = new Parser({
grammar: typescriptGrammar
})
const parser = new Parser(typescriptGrammar, Lexer.create('{'))

let error
try {
parser.parseText('{')
parser.parse()
} catch (err) {
error = err
}
Expand All @@ -44,13 +41,11 @@ describe('Parser', () => {
})

it('should return token of error with `EarlyEndOfParseError.getToken`', () => {
const parser = new Parser({
grammar: typescriptGrammar
})
const parser = new Parser(typescriptGrammar, Lexer.create('name]'))

let error
try {
parser.parseText('name]')
parser.parse()
} catch (err) {
error = err
}
Expand Down
5 changes: 1 addition & 4 deletions test/SpecialTypesParslet.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@ class BadParser extends Parser {
describe('specialTypesParslet', () => {
it('Errs with bad parser `consume()`', () => {
expect(() => {
const parser = new BadParser({
grammar: typescriptGrammar
})
parser.parseText('null')
const parser = new BadParser(typescriptGrammar, 'null')
specialTypesParslet(parser, Precedence.ALL, null)
}).to.throw('Unacceptable token:')
})
Expand Down
23 changes: 12 additions & 11 deletions test/api.errors.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,18 @@ describe('API errors', () => {
// Exclude name parslet
pathGrammar: namePathGrammar
})
const parser = new Parser({
grammar: [
const parser = new Parser(
[
// These are all we need here
namePathParslet,
createNameParslet({
allowedAdditionalTokens: []
})
]
})
],
'aaa.module:bbb'
)

parser.parseText('aaa.module:bbb')
parser.parse()
}).to.throw("Unexpected type: 'JsdocTypeSpecialNamePath'. Message: Type 'JsdocTypeSpecialNamePath' is only allowed with specialType 'event'")
})

Expand All @@ -78,17 +79,17 @@ describe('API errors', () => {
// Exclude name parslet
pathGrammar: namePathGrammar
})
const parser = new Parser({
grammar: [
const parser = new Parser(
[
// These are all we need here
namePathParslet,
createNameParslet({
allowedAdditionalTokens: []
})
]
})

parser.parseText('aaa.null')
],
'aaa.null'
)
parser.parse()
}).to.throw("Unexpected type: 'JsdocTypeNull'. Message: Expecting 'JsdocTypeName', 'JsdocTypeNumber', 'JsdocStringValue' or 'JsdocTypeSpecialNamePath")
})
})
Expand Down
21 changes: 6 additions & 15 deletions test/lexer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,22 +141,13 @@ describe('lexer', () => {
])
})

it('should throw when invoking token without first advancing', () => {
const lexer = new Lexer()
expect(() => {
lexer.token()
}).to.throw('Lexer not lexing')
})

it('should obtain last token', () => {
const text = '(null)'
const lexer = new Lexer()
lexer.lex(text)
const token1 = lexer.token()
lexer.advance()
const token2 = lexer.token()
lexer.advance()
const token3 = lexer.last() as Token
let lexer = Lexer.create('(null)')
const token1 = lexer.current
lexer = lexer.advance()
const token2 = lexer.current
lexer = lexer.advance()
const token3 = lexer.previous as Token
expect(token1.text).to.equal('(')
expect(token2.text).to.equal('null')
expect(token3.text).to.equal('null')
Expand Down

0 comments on commit 8118169

Please sign in to comment.