Skip to content

Commit

Permalink
perf(ternary-operator-parser): improve performance
Browse files Browse the repository at this point in the history
  • Loading branch information
kantord committed Dec 23, 2018
1 parent df5b91d commit cc324d1
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 52 deletions.
30 changes: 0 additions & 30 deletions src/__tests__/__snapshots__/interpreter.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -5965,17 +5965,7 @@ exports[`interpreter correct target tree 3 + 4 if 2 <= 3 else -12 1`] = `
Object {
"status": true,
"value": Object {
"end": Object {
"column": 25,
"line": 1,
"offset": 24,
},
"name": "ternary",
"start": Object {
"column": 1,
"line": 1,
"offset": 0,
},
"value": Object {
"left": Object {
"name": "binaryOperation",
Expand Down Expand Up @@ -6047,17 +6037,7 @@ Object {
"name": "pipe",
"value": Object {
"left": Object {
"end": Object {
"column": 25,
"line": 1,
"offset": 24,
},
"name": "ternary",
"start": Object {
"column": 1,
"line": 1,
"offset": 0,
},
"value": Object {
"left": Object {
"name": "tuple",
Expand Down Expand Up @@ -6417,17 +6397,7 @@ exports[`interpreter correct target tree error "Hello" if false else "asdfgb" 1`
Object {
"status": true,
"value": Object {
"end": Object {
"column": 37,
"line": 1,
"offset": 36,
},
"name": "ternary",
"start": Object {
"column": 1,
"line": 1,
"offset": 0,
},
"value": Object {
"left": Object {
"name": "functionCall",
Expand Down
3 changes: 2 additions & 1 deletion src/__tests__/descriptions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ const exclude = [
'program.js',
'operands',
'collections',
'pipe.js'
'pipe.js',
'ternary.js'
]
const files = fs
.readdirSync(path.join(__dirname, '..', 'parsers'))
Expand Down
7 changes: 7 additions & 0 deletions src/parsers/__tests__/ternary.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,11 @@ describe('ternary parser', () => {
}
})
})

it('handles tuple correctly', () => {
expect(parser.parse('0').value).toMatchObject({
name: 'primitive',
value: '0'
})
})
})
8 changes: 3 additions & 5 deletions src/parsers/pipe.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@ import P from 'parsimmon'
import type { NodeType } from '../types'

const PipeParser = P.lazy((): mixed => {
const TupleParser = require('./collections/tuple').default
const TernaryParser = require('./ternary').default
return P.alt(TernaryParser, TupleParser)
.sepBy1(P.regexp(/\s*\|\s*/))
.map((value: Array<NodeType>): NodeType =>
return TernaryParser.sepBy1(P.regexp(/\s*\|\s*/)).map(
(value: Array<NodeType>): NodeType =>
value.slice(1).reduce(
(a: NodeType, b: NodeType): NodeType => ({
name: 'pipe',
Expand All @@ -20,7 +18,7 @@ const PipeParser = P.lazy((): mixed => {
}),
value[0]
)
)
)
})

export default PipeParser
44 changes: 28 additions & 16 deletions src/parsers/ternary.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,38 @@
import P from 'parsimmon'
import crap from './crap'

import type { TupleNodeType, TernaryNodeValueType, ParserType } from '../types'
import type {
TupleNodeType,
ParserType,
NodeType
} from '../types'

export default P.lazy((): ParserType => {
const TupleParser = require('./collections/tuple').default
return P.seq(
TupleParser,
P.string('if')
.trim(crap)
.then(TupleParser),
P.string('else')
.trim(crap)
.then(TupleParser)
P.seq(
P.string('if')
.trim(crap)
.then(TupleParser),
P.string('else')
.trim(crap)
.then(TupleParser)
).atMost(1)
).map(
([tuple, rest]: [
TupleNodeType,
Array<[TupleNodeType, TupleNodeType]>
]): NodeType =>
rest.length === 0
? tuple
: {
name: 'ternary',
value: {
left: tuple,
middle: rest[0][0],
right: rest[0][1]
}
}
)
.map(
([left, middle, right]: [
TupleNodeType,
TupleNodeType,
TupleParser
]): TernaryNodeValueType => ({ left, middle, right })
)
.node('ternary')
.desc('ternary')
})

0 comments on commit cc324d1

Please sign in to comment.