Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// @flow
import P from 'parsimmon'
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]
}))
.desc('parentheses')
})
// @flow
import P from 'parsimmon'
import crap from './crap'
import type {
FunctionCallLambdaNodeType,
NodeType,
IdentifierNodeType
} from '../types'
const FunctionCallLambdaParser = P.lazy((): mixed => {
const TupleParser = require('./tuple/tuple').default
const IdentifierParser = require('./identifier').default
return P.seq(
IdentifierParser,
crap,
P.alt(
P.string('$')
.then(crap)
.then(P.string('=>'))
.then(crap),
P.string('\\')
),
crap,
TupleParser
).map(
([left, _, __, ___, right]: [
const BinaryOperatorParser = (
OperandParser: ParserType,
OperatorParser: ParserType,
description: string
): ParserType =>
P.lazy((): mixed => {
const OperatorParser_ = OperatorParser.node('primitive')
return P.seq(
OperandParser,
P.seq(crap.then(OperatorParser_), crap.then(OperandParser)).many()
)
.map(
(value: [NodeType, Array]): NodeType =>
value[1].length > 0
? {
name: 'binaryOperation',
value: value[1].reduce(
(a: Array, b: NodeType): Array =>
a.concat(b),
[value[0]]
)
}
return PatternSimple;
},
Match: function() {
return P.alt(
PatternLiteral,
PatternSimple,
PatternParenExpr
);
}
};
if (!helpers.hasOwnProperty(type)) {
throw new Error("cannot make pattern factory for " + type);
}
var PatternLeaf = P.lazy(helpers[type]);
var PatternSimple =
ione(ast.PatternSimple, ps.Identifier);
var PatternParenExpr =
ione(ast.PatternParenExpr, ps.ParenExpr);
var PatternLiteral =
ione(ast.PatternLiteral,
P.alt(
ps.Number,
ps.String,
ps.NamedLiteral
));
var PatternObjectPairBasic =
var star = Parsimmon.string('*');
var qmark = Parsimmon.string('?');
var bslash = Parsimmon.string('\\');
var suffixOperator = (operator, fn) => parser => parser.skip(operator).markMap(fn);
var plussed = suffixOperator(plus, oneOrMore);
var starred = suffixOperator(star, zeroOrMore);
var qmarked = suffixOperator(qmark, zeroOrOne);
var parenthesized = parser => lparen.then(parser).skip(rparen);
var bracketed = parser => lsqrb.then(parser).skip(rsqrb);
var bslashed = (string, map) => Parsimmon.string(`\\${string}`).markMap(map);
var escape = c => bslashed(c, exprs.atom(c)).markMap(quote).markMap(charEquals);
var sepBy = function(parser, separator) {
var pairs = separator.then(parser).many();
return parser.chain(r => pairs.map(rs => [r].concat(rs)));
};
var rootRegex = Parsimmon.lazy('root', () => regex.markMap(root));
var regex = Parsimmon.lazy('regex', () => sepBy(branch, pipe).markMap(alternation));
var wildcard = dot.markMap(wildcard);
var charPattern = /[^\\*+?()\[\].|]/;
var character = Parsimmon.regex(charPattern).markMap(exprs.atom).markMap(quote).markMap(charEquals);
var escChars = ['(', ')', '[', ']', '*', '+', '\\', '.', '|'];
var escaped = Parsimmon.alt.apply(null, escChars.map(escape));
var specialCharClass = Parsimmon.alt(
bslashed('s', whitespace),
bslashed('w', word),
bslashed('n', newline),
bslashed('d', digit)
);
var customClassChar = Parsimmon.regex(/[^\[\]\\]/);
var escapedBrackets = Parsimmon.alt(escape('['), escape(']'));
var escapedDash = escape('-');
var escapedBslash = escape('\\');
const rbracket = lexme(string(']'));
const blockterminator = lexme(string(';'));
const path = lexme(regex(/[a-zA-Z0-9._~!$&'()*+,;=:@%/-]+/)).desc('file-path');
const str = lexme(regex(/["']((?:\\.|.|\n)*?)["']/, 1)).desc('quoted string');
const booleanTrue = lexme(string('true')).desc('boolean true literal');
const booleanFalse = lexme(string('false')).desc('boolean false literal');
const number = lexme(regex(/[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?|[-+]?Infinity/)).desc(
'number literal'
);
const wordLiteral = lexme(regex(/[a-zA-Z0-9.<>+=\\*%!\|&/_-]+/)).desc('word literal');
const load = lexme(string('@load'));
const arrow = lexme(string('->'));
const fatArrow = lexme(string('->>'));
const colon = lexme(string(':')).desc('colon-char');
const primitive = lazy('primivite values', function() {
return alt(primitiveStringParser, primitiveBooleanParser, primitiveNumberParser);
});
const value = lazy('top level-values', function() {
return alt(primitive, quotationParser);
});
const words = lazy('words', function() {
return alt(value, setGlobalVarParser, setVarParser, wordLiteralParser);
});
const expressions = lazy('top-level expressions', function() {
return alt(definitionParser, words, loadDirectiveParser);
});
const primitiveStringParser = str.map(primitiveString);
function patternFactory(type, ps) {
var Pattern = P.lazy(function() {
return P.alt(
PatternArray,
PatternObject,
PatternLeaf
);
});
var helpers = {
Let: function() {
return PatternSimple;
},
Match: function() {
return P.alt(
PatternLiteral,
PatternSimple,
PatternParenExpr
function fix(f) {
let lazy
lazy = f(P.lazy(() => {return lazy}))
return lazy
}
const SpreadParser = P.string('...')
.then(crap)
.then(TupleParser)
.node('spread')
const SimpleListParser = P.sepBy(TupleParser, SeparatorParser).node(
'simpleList'
)
return P.sepBy(P.alt(SpreadParser, SimpleListParser), SeparatorParser).map(
(value: CollectionCoreValueType): CollectionCoreNodeType => ({
name: 'collectionCore',
value
})
)
})
const ComprehensionParser = P.lazy((): mixed => {
const TupleParser = require('./tuple').default
const ProgramParser = require('../program').default
return P.seq(
P.string('each')
.then(crap)
.then(TupleParser),
crap.then(P.string('in')).then(crap.then(TupleParser)),
crap
.then(P.string('if'))
.then(crap.then(TupleParser))
.atMost(1),
crap.then(ProgramParser).atMost(1)
).map(
([left, middle, condition, right]: [
TupleNodeType,
TupleNodeType,
keys.forEach(function(k) {
var f = almostParsers[k];
var g = f.bind(null, parsers);
parsers[k] = P.lazy(g);
});
Object.freeze(parsers);