tfl-js
Version:
A TypeScript library for parsing and evaluating propositional logic formulas
162 lines • 4.82 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.TFLLexer = exports.Tokens = void 0;
const chevrotain_1 = require("chevrotain");
const formula_1 = require("../types/formula");
// Create base category tokens
const NotCategory = (0, chevrotain_1.createToken)({ name: 'Not', pattern: chevrotain_1.Lexer.NA });
const AndCategory = (0, chevrotain_1.createToken)({ name: 'And', pattern: chevrotain_1.Lexer.NA });
const OrCategory = (0, chevrotain_1.createToken)({ name: 'Or', pattern: chevrotain_1.Lexer.NA });
const IfCategory = (0, chevrotain_1.createToken)({ name: 'If', pattern: chevrotain_1.Lexer.NA });
const IffCategory = (0, chevrotain_1.createToken)({ name: 'Iff', pattern: chevrotain_1.Lexer.NA });
// Create tokens for different syntax styles
const WhiteSpace = (0, chevrotain_1.createToken)({
name: 'WhiteSpace',
pattern: /\s+/,
group: chevrotain_1.Lexer.SKIPPED
});
// Atomic propositions
const AtomicProp = (0, chevrotain_1.createToken)({
name: 'AtomicProp',
pattern: /[A-Za-z][0-9]*/
});
// Parentheses
const LParen = (0, chevrotain_1.createToken)({ name: 'LParen', pattern: /\(/ });
const RParen = (0, chevrotain_1.createToken)({ name: 'RParen', pattern: /\)/ });
// Standard notation
const StandardNot = (0, chevrotain_1.createToken)({
name: 'StandardNot',
pattern: new RegExp(formula_1.Operator.NOT),
longer_alt: AtomicProp,
categories: [NotCategory]
});
const StandardAnd = (0, chevrotain_1.createToken)({
name: 'StandardAnd',
pattern: new RegExp('\\' + formula_1.Operator.AND),
categories: [AndCategory]
});
const StandardOr = (0, chevrotain_1.createToken)({
name: 'StandardOr',
pattern: new RegExp('\\' + formula_1.Operator.OR),
categories: [OrCategory]
});
const StandardIf = (0, chevrotain_1.createToken)({
name: 'StandardIf',
pattern: new RegExp('\\' + formula_1.Operator.IF),
categories: [IfCategory]
});
const StandardIff = (0, chevrotain_1.createToken)({
name: 'StandardIff',
pattern: new RegExp('\\' + formula_1.Operator.IFF),
categories: [IffCategory]
});
// TFL notation
const TflNot = (0, chevrotain_1.createToken)({
name: 'TflNot',
pattern: /~/,
categories: [NotCategory]
});
const TflAnd = (0, chevrotain_1.createToken)({
name: 'TflAnd',
pattern: /&/,
categories: [AndCategory]
});
const TflOr = (0, chevrotain_1.createToken)({
name: 'TflOr',
pattern: /v|∨/,
categories: [OrCategory]
});
const TflIf = (0, chevrotain_1.createToken)({
name: 'TflIf',
pattern: /->/,
categories: [IfCategory]
});
const TflIff = (0, chevrotain_1.createToken)({
name: 'TflIff',
pattern: /<->/,
categories: [IffCategory]
});
// English notation
const EnglishNot = (0, chevrotain_1.createToken)({
name: 'EnglishNot',
pattern: /not\s/,
categories: [NotCategory]
});
const EnglishAnd = (0, chevrotain_1.createToken)({
name: 'EnglishAnd',
pattern: /and\s/,
categories: [AndCategory]
});
const EnglishOr = (0, chevrotain_1.createToken)({
name: 'EnglishOr',
pattern: /or\s/,
categories: [OrCategory]
});
const EnglishIf = (0, chevrotain_1.createToken)({
name: 'EnglishIf',
pattern: /(if|only\sif)\s/,
categories: [IfCategory]
});
const EnglishIff = (0, chevrotain_1.createToken)({
name: 'EnglishIff',
pattern: /if\sand\sonly\sif\s/,
categories: [IffCategory]
});
// Export all tokens
exports.Tokens = {
WhiteSpace,
AtomicProp,
LParen,
RParen,
StandardNot,
StandardAnd,
StandardOr,
StandardIf,
StandardIff,
TflNot,
TflAnd,
TflOr,
TflIf,
TflIff,
EnglishNot,
EnglishAnd,
EnglishOr,
EnglishIf,
EnglishIff
};
// Create array of token types in order of precedence
const tokenList = [
exports.Tokens.WhiteSpace,
// English notation (must come before atomic props due to 'if', 'and', etc.)
exports.Tokens.EnglishIff,
exports.Tokens.EnglishIf,
exports.Tokens.EnglishAnd,
exports.Tokens.EnglishOr,
exports.Tokens.EnglishNot,
// Standard notation
exports.Tokens.StandardNot,
exports.Tokens.StandardAnd,
exports.Tokens.StandardOr,
exports.Tokens.StandardIf,
exports.Tokens.StandardIff,
// TFL notation
exports.Tokens.TflNot,
exports.Tokens.TflAnd,
exports.Tokens.TflOr,
exports.Tokens.TflIf,
exports.Tokens.TflIff,
// Basic tokens
exports.Tokens.AtomicProp,
exports.Tokens.LParen,
exports.Tokens.RParen
];
// Create the lexer
exports.TFLLexer = new chevrotain_1.Lexer(tokenList, {
// Enable error recovery
recoveryEnabled: true,
// Ensure longer tokens are matched before shorter ones
ensureOptimizations: true,
// Skip comments and whitespace
skipValidations: false
});
//# sourceMappingURL=lexer.js.map