good-enough-parser
Advanced tools
Comparing version 1.0.1 to 1.1.0
@@ -12,5 +12,5 @@ import type { Cursor } from '../parser/types'; | ||
} | ||
declare type LanguagePreset = 'python'; | ||
declare type LanguagePreset = 'groovy' | 'python'; | ||
export declare function createLang(key: LanguagePreset): Language; | ||
export declare function createLang(config: LanguageConfig): Language; | ||
//# sourceMappingURL=index.d.ts.map |
@@ -18,2 +18,3 @@ "use strict"; | ||
const util_1 = require("../query/util"); | ||
const groovy_1 = require("./groovy"); | ||
const python_1 = require("./python"); | ||
@@ -40,3 +41,6 @@ __exportStar(require("./types"), exports); | ||
exports.Language = Language; | ||
const languagePresets = { python: python_1.lang }; | ||
const languagePresets = { | ||
groovy: groovy_1.lang, | ||
python: python_1.lang, | ||
}; | ||
function createLang(arg1) { | ||
@@ -43,0 +47,0 @@ const config = typeof arg1 === 'string' ? languagePresets[arg1] : arg1; |
@@ -33,13 +33,12 @@ "use strict"; | ||
} | ||
function varTplState($, strState, { allowedTokens = [], tplStateName }) { | ||
function varTplState($, strState, { operators = [], symbols }) { | ||
const result = { ...strState }; | ||
Object.entries(result).forEach(([key, val]) => { | ||
if (val.t !== 'fallback') { | ||
const { push, pop, next } = val; | ||
if ([push, pop, next].every((x) => x === undefined)) { | ||
result[key] = { ...val, next: tplStateName }; | ||
const rule = { ...val }; | ||
if (rule.push) { | ||
rule.next = rule.push; | ||
delete rule.push; | ||
} | ||
else { | ||
result[key] = { ...val }; | ||
} | ||
result[key] = rule; | ||
} | ||
@@ -50,11 +49,17 @@ else { | ||
}); | ||
for (const tokenName of allowedTokens) { | ||
const rule = $[tokenName]; | ||
if (rule) { | ||
if (rule.t !== 'fallback') { | ||
result[tokenName] = { ...rule }; | ||
} | ||
const symbolRule = symbols | ||
? { t: 'regex', match: symbols } | ||
: $.symbol; | ||
if (!symbolRule) { | ||
throw new Error(`String definition isn't found for template definition`); | ||
} | ||
result.symbol = symbolRule; | ||
for (const op of operators) { | ||
const opEntry = Object.entries($).find(([key, rule]) => key.startsWith('op$') && rule.t === 'string' && rule.match === op); | ||
if (opEntry) { | ||
const [opKey, opRule] = opEntry; | ||
result[opKey] = { ...opRule }; | ||
} | ||
else { | ||
throw new Error(`Wrong element for allowedRules: ${tokenName}`); | ||
throw new Error(`Operator is not found: ${op}`); | ||
} | ||
@@ -104,5 +109,6 @@ } | ||
}; | ||
const { allowedTokens } = tplOpt; | ||
const { operators = [], symbols } = tplOpt; | ||
varTplPreStates.push({ | ||
allowedTokens, | ||
symbols, | ||
operators, | ||
tplStateName, | ||
@@ -109,0 +115,0 @@ tplStart, |
@@ -18,3 +18,4 @@ export interface OptionBase { | ||
type: 'var'; | ||
allowedTokens: string[]; | ||
symbols?: RegExp; | ||
operators?: string[]; | ||
} | ||
@@ -21,0 +22,0 @@ export interface ExpressionTemplateOption extends OptionBase { |
@@ -38,3 +38,4 @@ "use strict"; | ||
let nestingCounter = 0; | ||
for (const token of lexer) { | ||
const lex = [...lexer]; | ||
for (const token of lex) { | ||
if (!currentTree.children.length) { | ||
@@ -135,3 +136,4 @@ currentTree.children.push(specialToken('_start', prevToken, token)); | ||
} | ||
else if (token.type === 'template-start') { | ||
else if (token.type === 'template-start' && | ||
currentTree.type === 'string-tree') { | ||
stack.push(currentTree); | ||
@@ -157,2 +159,56 @@ currentTree = { | ||
} | ||
else if (token.type === 'string-value' && | ||
currentTree.type === 'template-tree') { | ||
const tplTree = currentTree; | ||
const tplEndToken = specialToken('_end', prevToken, token); | ||
tplTree.children.push(tplEndToken); | ||
tplTree.endsWith = { ...tplEndToken, type: 'template-end' }; | ||
const upperTree = stack.pop(); | ||
if (upperTree) { | ||
upperTree.children.push(tplTree); | ||
upperTree.children.push(token); | ||
currentTree = upperTree; | ||
nestingCounter -= 1; | ||
} | ||
} | ||
else if (token.type === 'string-end' && | ||
currentTree.type === 'template-tree') { | ||
const tplTree = currentTree; | ||
const tplEndToken = specialToken('_end', prevToken, token); | ||
tplTree.children.push(tplEndToken); | ||
tplTree.endsWith = { ...tplEndToken, type: 'template-end' }; | ||
const strTree = stack.pop(); | ||
if ((strTree === null || strTree === void 0 ? void 0 : strTree.type) === 'string-tree') { | ||
strTree.children.push(tplTree); | ||
const strEndToken = specialToken('_end', prevToken, token); | ||
strTree.children.push(strEndToken); | ||
strTree.endsWith = token; | ||
currentTree = strTree; | ||
nestingCounter -= 1; | ||
const upperTree = stack.pop(); | ||
if (upperTree) { | ||
upperTree.children.push(strTree); | ||
currentTree = upperTree; | ||
nestingCounter -= 1; | ||
} | ||
} | ||
} | ||
else if (token.type === 'template-start' && | ||
currentTree.type === 'template-tree') { | ||
const tplTree = currentTree; | ||
const tplEndToken = specialToken('_end', prevToken, token); | ||
tplTree.children.push(tplEndToken); | ||
tplTree.endsWith = { ...tplEndToken, type: 'template-end' }; | ||
const strTree = stack.pop(); | ||
if ((strTree === null || strTree === void 0 ? void 0 : strTree.type) === 'string-tree') { | ||
strTree.children.push(tplTree); | ||
stack.push(strTree); | ||
currentTree = { | ||
type: 'template-tree', | ||
startsWith: token, | ||
endsWith: { ...token, type: 'template-end' }, | ||
children: [], | ||
}; | ||
} | ||
} | ||
else { | ||
@@ -159,0 +215,0 @@ currentTree.children.push(token); |
@@ -12,5 +12,5 @@ import type { Cursor } from '../parser/types'; | ||
} | ||
declare type LanguagePreset = 'python'; | ||
declare type LanguagePreset = 'groovy' | 'python'; | ||
export declare function createLang(key: LanguagePreset): Language; | ||
export declare function createLang(config: LanguageConfig): Language; | ||
//# sourceMappingURL=index.d.ts.map |
@@ -5,2 +5,3 @@ import { createLexer } from '../lexer'; | ||
import { clone } from '../query/util'; | ||
import { lang as groovy } from './groovy'; | ||
import { lang as python } from './python'; | ||
@@ -26,3 +27,6 @@ export * from './types'; | ||
} | ||
const languagePresets = { python }; | ||
const languagePresets = { | ||
groovy, | ||
python, | ||
}; | ||
export function createLang(arg1) { | ||
@@ -29,0 +33,0 @@ const config = typeof arg1 === 'string' ? languagePresets[arg1] : arg1; |
@@ -30,13 +30,12 @@ import { getCounterpartBracketKey, isBracketKey, isRightKey } from './bracket'; | ||
} | ||
function varTplState($, strState, { allowedTokens = [], tplStateName }) { | ||
function varTplState($, strState, { operators = [], symbols }) { | ||
const result = { ...strState }; | ||
Object.entries(result).forEach(([key, val]) => { | ||
if (val.t !== 'fallback') { | ||
const { push, pop, next } = val; | ||
if ([push, pop, next].every((x) => x === undefined)) { | ||
result[key] = { ...val, next: tplStateName }; | ||
const rule = { ...val }; | ||
if (rule.push) { | ||
rule.next = rule.push; | ||
delete rule.push; | ||
} | ||
else { | ||
result[key] = { ...val }; | ||
} | ||
result[key] = rule; | ||
} | ||
@@ -47,11 +46,17 @@ else { | ||
}); | ||
for (const tokenName of allowedTokens) { | ||
const rule = $[tokenName]; | ||
if (rule) { | ||
if (rule.t !== 'fallback') { | ||
result[tokenName] = { ...rule }; | ||
} | ||
const symbolRule = symbols | ||
? { t: 'regex', match: symbols } | ||
: $.symbol; | ||
if (!symbolRule) { | ||
throw new Error(`String definition isn't found for template definition`); | ||
} | ||
result.symbol = symbolRule; | ||
for (const op of operators) { | ||
const opEntry = Object.entries($).find(([key, rule]) => key.startsWith('op$') && rule.t === 'string' && rule.match === op); | ||
if (opEntry) { | ||
const [opKey, opRule] = opEntry; | ||
result[opKey] = { ...opRule }; | ||
} | ||
else { | ||
throw new Error(`Wrong element for allowedRules: ${tokenName}`); | ||
throw new Error(`Operator is not found: ${op}`); | ||
} | ||
@@ -101,5 +106,6 @@ } | ||
}; | ||
const { allowedTokens } = tplOpt; | ||
const { operators = [], symbols } = tplOpt; | ||
varTplPreStates.push({ | ||
allowedTokens, | ||
symbols, | ||
operators, | ||
tplStateName, | ||
@@ -106,0 +112,0 @@ tplStart, |
@@ -18,3 +18,4 @@ export interface OptionBase { | ||
type: 'var'; | ||
allowedTokens: string[]; | ||
symbols?: RegExp; | ||
operators?: string[]; | ||
} | ||
@@ -21,0 +22,0 @@ export interface ExpressionTemplateOption extends OptionBase { |
@@ -35,3 +35,4 @@ function specialToken(type, prevToken, nextToken) { | ||
let nestingCounter = 0; | ||
for (const token of lexer) { | ||
const lex = [...lexer]; | ||
for (const token of lex) { | ||
if (!currentTree.children.length) { | ||
@@ -132,3 +133,4 @@ currentTree.children.push(specialToken('_start', prevToken, token)); | ||
} | ||
else if (token.type === 'template-start') { | ||
else if (token.type === 'template-start' && | ||
currentTree.type === 'string-tree') { | ||
stack.push(currentTree); | ||
@@ -154,2 +156,56 @@ currentTree = { | ||
} | ||
else if (token.type === 'string-value' && | ||
currentTree.type === 'template-tree') { | ||
const tplTree = currentTree; | ||
const tplEndToken = specialToken('_end', prevToken, token); | ||
tplTree.children.push(tplEndToken); | ||
tplTree.endsWith = { ...tplEndToken, type: 'template-end' }; | ||
const upperTree = stack.pop(); | ||
if (upperTree) { | ||
upperTree.children.push(tplTree); | ||
upperTree.children.push(token); | ||
currentTree = upperTree; | ||
nestingCounter -= 1; | ||
} | ||
} | ||
else if (token.type === 'string-end' && | ||
currentTree.type === 'template-tree') { | ||
const tplTree = currentTree; | ||
const tplEndToken = specialToken('_end', prevToken, token); | ||
tplTree.children.push(tplEndToken); | ||
tplTree.endsWith = { ...tplEndToken, type: 'template-end' }; | ||
const strTree = stack.pop(); | ||
if ((strTree === null || strTree === void 0 ? void 0 : strTree.type) === 'string-tree') { | ||
strTree.children.push(tplTree); | ||
const strEndToken = specialToken('_end', prevToken, token); | ||
strTree.children.push(strEndToken); | ||
strTree.endsWith = token; | ||
currentTree = strTree; | ||
nestingCounter -= 1; | ||
const upperTree = stack.pop(); | ||
if (upperTree) { | ||
upperTree.children.push(strTree); | ||
currentTree = upperTree; | ||
nestingCounter -= 1; | ||
} | ||
} | ||
} | ||
else if (token.type === 'template-start' && | ||
currentTree.type === 'template-tree') { | ||
const tplTree = currentTree; | ||
const tplEndToken = specialToken('_end', prevToken, token); | ||
tplTree.children.push(tplEndToken); | ||
tplTree.endsWith = { ...tplEndToken, type: 'template-end' }; | ||
const strTree = stack.pop(); | ||
if ((strTree === null || strTree === void 0 ? void 0 : strTree.type) === 'string-tree') { | ||
strTree.children.push(tplTree); | ||
stack.push(strTree); | ||
currentTree = { | ||
type: 'template-tree', | ||
startsWith: token, | ||
endsWith: { ...token, type: 'template-end' }, | ||
children: [], | ||
}; | ||
} | ||
} | ||
else { | ||
@@ -156,0 +212,0 @@ currentTree.children.push(token); |
{ | ||
"name": "good-enough-parser", | ||
"description": "Parse and query computer programs source code", | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"repository": "git@github.com:zharinov/good-enough-parser.git", | ||
@@ -6,0 +6,0 @@ "author": "Sergei Zharinov", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
370743
275
4945