Skip to content

Commit

Permalink
feat: add descriptions to parsers
Browse files Browse the repository at this point in the history
fixes #114
  • Loading branch information
kantord committed Nov 2, 2018
1 parent 1b071e0 commit d49162d
Show file tree
Hide file tree
Showing 17 changed files with 110 additions and 53 deletions.
38 changes: 38 additions & 0 deletions src/__tests__/descriptions.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// @flow

const fs = require('fs')
const path = require('path')

const exclude = [
'__tests__',
'abstract',
'pipe',
'tuple',
'primitives',
'operand.js',
'listCore.js',
'crap.js',
'functionCall.js',
'parser.js',
'primitive.js',
'value.js',
'section.js',
'projectable.js',
'program.js'
]
const files = fs
.readdirSync(path.join(__dirname, '..', 'parsers'))
.filter((item: string): boolean => !exclude.includes(item))

describe('descriptions', () => {
files.forEach((fileName: string) => {
const parserName = fileName.slice(0, -3)
const moduleName = `../parsers/${parserName}`
// $FlowFixMe
const parser = require(moduleName).default._
const options = parser('¡').expected
it(parserName, () => {
expect(options[0]).toEqual(parserName)
})
})
})
33 changes: 17 additions & 16 deletions src/parsers/abstract/binaryOperator.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,29 @@ import type { NodeType, ParserType } from '../../types'

const BinaryOperatorParser = (
OperandParser: ParserType,
OperatorParser: ParserType
OperatorParser: ParserType,
description: string
): ParserType =>
P.lazy((): mixed => {
const OperatorParser_ = OperatorParser.node(
'primitive'
)
const OperatorParser_ = OperatorParser.node('primitive')
return P.seq(
OperandParser,
P.seq(crap.then(OperatorParser_), crap.then(OperandParser)).many()
).map(
(value: [NodeType, Array<NodeType>]): NodeType =>
value[1].length > 0
? {
name: 'binaryOperation',
value: value[1].reduce(
(a: Array<NodeType>, b: NodeType): Array<NodeType> =>
a.concat(b),
[value[0]]
)
}
: value[0]
)
.map(
(value: [NodeType, Array<NodeType>]): NodeType =>
value[1].length > 0
? {
name: 'binaryOperation',
value: value[1].reduce(
(a: Array<NodeType>, b: NodeType): Array<NodeType> =>
a.concat(b),
[value[0]]
)
}
: value[0]
)
.desc(description)
})

export default BinaryOperatorParser
1 change: 1 addition & 0 deletions src/parsers/abstract/listLike.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ export default ({
.wrap(P.string(open), P.string(close))
.map((value: {value: ListCoreNodeType}): ListCoreNodeType => value.value)
.node(name)
.desc(name)
6 changes: 5 additions & 1 deletion src/parsers/additive.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,8 @@ import P from 'parsimmon'
import MultiplicativeParser from './multiplicative'
import BinaryOperatorParser from './abstract/binaryOperator'

export default BinaryOperatorParser(MultiplicativeParser, P.regexp(/[+-]/))
export default BinaryOperatorParser(
MultiplicativeParser,
P.regexp(/[+-]/),
'additive'
)
1 change: 1 addition & 0 deletions src/parsers/assignment.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,4 @@ export default P.seq(
})
)
.node('assignment')
.desc('assignment')
6 changes: 5 additions & 1 deletion src/parsers/boolean1.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,8 @@ import P from 'parsimmon'
import AdditiveParser from './additive'
import BinaryOperatorParser from './abstract/binaryOperator'

export default BinaryOperatorParser(AdditiveParser, P.regexp(/[<>]=?/))
export default BinaryOperatorParser(
AdditiveParser,
P.regexp(/[<>]=?/),
'boolean1'
)
3 changes: 2 additions & 1 deletion src/parsers/boolean2.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ import BinaryOperatorParser from './abstract/binaryOperator'

export default BinaryOperatorParser(
Boolean1,
P.regexp(/[=!]=/).map((x: string): string => x + '=')
P.regexp(/[=!]=/).map((x: string): string => x + '='),
'boolean2'
)
3 changes: 2 additions & 1 deletion src/parsers/boolean3.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ export default BinaryOperatorParser(
Boolean2,
P.regexp(/(\|\||&&|or|and)/).map(
(x: string): string => (x === 'or' ? '||' : x === 'and' ? '&&' : x)
)
),
'boolean3'
)
10 changes: 6 additions & 4 deletions src/parsers/identifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import type { IdentifierNodeType } from '../types'

export default P.regexp(
/((?!null|false|true)[a-zA-Z][0-9a-zA-Z_$]*|(null|false|true)[0-9a-zA-Z_$])/
).map((value: string): IdentifierNodeType => ({
name: 'identifier',
value
}))
)
.map((value: string): IdentifierNodeType => ({
name: 'identifier',
value
}))
.desc('identifier')
8 changes: 4 additions & 4 deletions src/parsers/inputProp.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
import P from 'parsimmon'
import type { InputPropNodeType } from '../types'

export default P.regexp(/\.[$A-Z_][0-9A-Z_$]*/i).map(
(value: string): InputPropNodeType => ({
export default P.regexp(/\.[$A-Z_][0-9A-Z_$]*/i)
.map((value: string): InputPropNodeType => ({
name: 'inputProp',
value
})
)
}))
.desc('inputProp')
1 change: 1 addition & 0 deletions src/parsers/lambda.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,4 @@ export default P.alt(
)
.trim(crap)
.node('lambda')
.desc('lambda')
3 changes: 2 additions & 1 deletion src/parsers/multiplicative.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ import BinaryOperatorParser from './abstract/binaryOperator'

export default BinaryOperatorParser(
P.alt(OperandParser, UnaryOperatorParser),
P.regexp(/[*/%]/)
P.regexp(/[*/%]/),
'multiplicative'
)
14 changes: 6 additions & 8 deletions src/parsers/parentheses.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@ import type { ParenthesesNodeType, ParserType } from '../types'

export default P.lazy((): ParserType => {
const ProgramParser = require('./program').default
return P.seq(
P.string('('),
ProgramParser,
P.string(')')
).map((value: [mixed, ParenthesesNodeType, mixed]): ParenthesesNodeType => ({
name: 'parentheses',
value: value[1]
}))
return P.seq(P.string('('), ProgramParser, P.string(')'))
.map((value: [mixed, ParenthesesNodeType, mixed]): ParenthesesNodeType => ({
name: 'parentheses',
value: value[1]
}))
.desc('parentheses')
})
2 changes: 1 addition & 1 deletion src/parsers/projection.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ const ProjectionParser = P.lazy((): mixed => {
P.alt(ProjectionParser, PropertyParser)
.skip(crap)
.atLeast(1)
).map(unpack)
).map(unpack).desc('projection')
})

export default ProjectionParser
1 change: 1 addition & 0 deletions src/parsers/ternary.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ export default P.lazy((): ParserType => {
]): TernaryNodeValueType => ({ left, middle, right })
)
.node('ternary')
.desc('ternary')
})
32 changes: 17 additions & 15 deletions src/parsers/unaryOperator.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,23 @@ const UnaryOperatorParser = P.lazy((): ParserType => {
.trim(crap)
.node('primitive')
const OperandParser = require('./operand').default
return P.seq(OperatorParser.atLeast(1), OperandParser).map(
([operators, operand]: [Array<PrimitiveNodeType>, NodeType]): NodeType =>
operators.length > 0
? operators.reduce(
(a: NodeType, b: PrimitiveNodeType): UnaryOperationNodeType => ({
name: 'unaryOperation',
value: {
operator: b,
operand: a
}
}),
operand
)
: operand
)
return P.seq(OperatorParser.atLeast(1), OperandParser)
.map(
([operators, operand]: [Array<PrimitiveNodeType>, NodeType]): NodeType =>
operators.length > 0
? operators.reduce(
(a: NodeType, b: PrimitiveNodeType): UnaryOperationNodeType => ({
name: 'unaryOperation',
value: {
operator: b,
operand: a
}
}),
operand
)
: operand
)
.desc('unaryOperator')
})

export default UnaryOperatorParser
1 change: 1 addition & 0 deletions src/parsers/variable.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ export default P.string('$')
name: 'variable',
value: value && value.length ? `$${value[0].value}` : '$'
}))
.desc('variable')

0 comments on commit d49162d

Please sign in to comment.