occam-grammars 1.3.169 → 1.3.171
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/example.js +324 -86
- package/lib/constants.js +17 -4
- package/lib/example/utilities/furtle.js +33 -0
- package/lib/example/view/furtle.js +14 -3
- package/lib/example/view.js +12 -10
- package/lib/furtle/bnf.js +2 -2
- package/lib/furtle/entries.js +2 -8
- package/lib/index.js +17 -25
- package/lib/utilities/lexers.js +23 -3
- package/lib/utilities/parsers.js +20 -1
- package/lib/utilities/query.js +89 -0
- package/package.json +7 -6
- package/src/constants.js +2 -0
- package/src/example/utilities/furtle.js +31 -0
- package/src/example/view/furtle.js +22 -3
- package/src/example/view.js +8 -8
- package/src/furtle/bnf.js +1 -53
- package/src/furtle/entries.js +1 -7
- package/src/index.js +0 -2
- package/src/utilities/lexers.js +34 -7
- package/src/utilities/parsers.js +27 -3
- package/src/utilities/query.js +89 -0
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "occam-grammars",
|
|
3
3
|
"author": "James Smith",
|
|
4
|
-
"version": "1.3.
|
|
4
|
+
"version": "1.3.171",
|
|
5
5
|
"license": "MIT, Anti-996",
|
|
6
6
|
"homepage": "https://github.com/djalbat/occam-grammars",
|
|
7
7
|
"description": "Occam's grammars.",
|
|
@@ -11,15 +11,16 @@
|
|
|
11
11
|
},
|
|
12
12
|
"dependencies": {
|
|
13
13
|
"necessary": "^14.3.2",
|
|
14
|
-
"occam-grammar-utilities": "^8.0.
|
|
15
|
-
"occam-lexers": "^23.0.
|
|
16
|
-
"occam-parsers": "^23.0.
|
|
14
|
+
"occam-grammar-utilities": "^8.0.259",
|
|
15
|
+
"occam-lexers": "^23.0.47",
|
|
16
|
+
"occam-parsers": "^23.0.51",
|
|
17
|
+
"occam-query": "^4.1.33"
|
|
17
18
|
},
|
|
18
19
|
"devDependencies": {
|
|
19
20
|
"@swc/core": "^1.5.6",
|
|
20
21
|
"easy": "^23.0.1",
|
|
21
|
-
"easy-layout": "^6.0.
|
|
22
|
-
"easy-with-style": "^3.0.
|
|
22
|
+
"easy-layout": "^6.0.215",
|
|
23
|
+
"easy-with-style": "^3.0.436",
|
|
23
24
|
"esbuild": "^0.11.5",
|
|
24
25
|
"express": "^4.17.1",
|
|
25
26
|
"juxtapose": "^4.0.117",
|
package/src/constants.js
CHANGED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
import { lexersUtilities, parsersUtilities } from "../../index";
|
|
4
|
+
import { rulesUtilities as lexersRulesUtilities } from "occam-lexers";
|
|
5
|
+
import { rulesUtilities as parsersRulesUtilities } from "occam-parsers";
|
|
6
|
+
|
|
7
|
+
const { rulesAsEntries } = lexersRulesUtilities,
|
|
8
|
+
{ rulesAsString, rulesFromStartRuleAndRuleMap } = parsersRulesUtilities;
|
|
9
|
+
|
|
10
|
+
const { furtleLexerFromNothing } = lexersUtilities,
|
|
11
|
+
{ furtleParserFromNothing } = parsersUtilities;
|
|
12
|
+
|
|
13
|
+
export function bnfFromNothing() {
|
|
14
|
+
const furtleParser = furtleParserFromNothing(),
|
|
15
|
+
startRule = furtleParser.getStartRule(),
|
|
16
|
+
ruleMap = furtleParser.getRuleMap(),
|
|
17
|
+
rules = rulesFromStartRuleAndRuleMap(startRule, ruleMap),
|
|
18
|
+
multiline = true,
|
|
19
|
+
string = rulesAsString(rules, multiline),
|
|
20
|
+
bnf = string; ///
|
|
21
|
+
|
|
22
|
+
return bnf;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function entriesFromNothing() {
|
|
26
|
+
const furtleLexer = furtleLexerFromNothing(),
|
|
27
|
+
rules = furtleLexer.getRules(),
|
|
28
|
+
entries = rulesAsEntries(rules);
|
|
29
|
+
|
|
30
|
+
return entries;
|
|
31
|
+
}
|
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
-
import {
|
|
3
|
+
import { lexersUtilities, parsersUtilities } from "../../index"; ///
|
|
4
4
|
|
|
5
5
|
import View from "../view";
|
|
6
6
|
|
|
7
|
+
import { bnfFromNothing, entriesFromNothing } from "../utilities/furtle";
|
|
8
|
+
|
|
7
9
|
const { furtleLexerFromEntries } = lexersUtilities,
|
|
8
10
|
{ furtleParserFromBNFAndStartRuleName } = parsersUtilities;
|
|
9
11
|
|
|
@@ -36,9 +38,26 @@ export default class FurtleView extends View {
|
|
|
36
38
|
return parseTree;
|
|
37
39
|
}
|
|
38
40
|
|
|
39
|
-
|
|
41
|
+
initialise() {
|
|
42
|
+
this.assignContext();
|
|
43
|
+
|
|
44
|
+
const { initialContent, initialRuleName } = this.constructor,
|
|
45
|
+
bnf = bnfFromNothing(),
|
|
46
|
+
entries = entriesFromNothing(),
|
|
47
|
+
content = initialContent, ///
|
|
48
|
+
ruleName = initialRuleName, ///
|
|
49
|
+
lexicalEntries = entries; ///
|
|
50
|
+
|
|
51
|
+
this.setBNF(bnf);
|
|
52
|
+
|
|
53
|
+
this.setContent(content);
|
|
40
54
|
|
|
41
|
-
|
|
55
|
+
this.setRuleName(ruleName);
|
|
56
|
+
|
|
57
|
+
this.setLexicalEntries(lexicalEntries);
|
|
58
|
+
|
|
59
|
+
this.keyUpHandler();
|
|
60
|
+
}
|
|
42
61
|
|
|
43
62
|
static readOnly = false;
|
|
44
63
|
|
package/src/example/view.js
CHANGED
|
@@ -16,20 +16,20 @@ import LexicalEntriesTextarea from "./view/textarea/lexicalEntries";
|
|
|
16
16
|
|
|
17
17
|
class View extends Element {
|
|
18
18
|
keyUpHandler = (event, element) => {
|
|
19
|
-
try {
|
|
19
|
+
// try {
|
|
20
20
|
const tokens = this.getTokens(),
|
|
21
21
|
parseTree = this.getParseTree(tokens);
|
|
22
22
|
|
|
23
23
|
this.setTokens(tokens);
|
|
24
24
|
|
|
25
25
|
this.setParseTree(parseTree);
|
|
26
|
-
} catch (error) {
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
}
|
|
26
|
+
// } catch (error) {
|
|
27
|
+
// console.log(error);
|
|
28
|
+
//
|
|
29
|
+
// this.clearTokens();
|
|
30
|
+
//
|
|
31
|
+
// this.clearParseTree();
|
|
32
|
+
// }
|
|
33
33
|
}
|
|
34
34
|
|
|
35
35
|
getTokens() {
|
package/src/furtle/bnf.js
CHANGED
|
@@ -128,58 +128,6 @@ reference. ::= [name] ;
|
|
|
128
128
|
|
|
129
129
|
|
|
130
130
|
|
|
131
|
-
nonsense. ::= [type] | [keyword] | [primitive] | [query] | [special] | [name] | [number] | [unassigned]
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
expression ::= path spread? subExpression? ;
|
|
136
|
-
|
|
137
|
-
path ::= "/" infiniteDescent? selectors ;
|
|
138
|
-
|
|
139
|
-
subExpression ::= path spread? subExpression?;
|
|
140
|
-
|
|
141
|
-
infiniteDescent ::= "/" ;
|
|
142
|
-
|
|
143
|
-
selectors ::= selector ( "|" selector )* ;
|
|
144
|
-
|
|
145
|
-
spread ::= unique
|
|
146
|
-
|
|
147
|
-
| "["
|
|
148
|
-
|
|
149
|
-
(
|
|
150
|
-
|
|
151
|
-
( startIndex "..." endIndex )
|
|
152
|
-
|
|
153
|
-
|
|
|
154
|
-
|
|
155
|
-
( startIndex "..." )
|
|
156
|
-
|
|
157
|
-
|
|
|
158
|
-
|
|
159
|
-
( "..." endIndex )
|
|
160
|
-
|
|
161
|
-
|
|
|
162
|
-
|
|
163
|
-
index
|
|
164
|
-
|
|
165
|
-
)
|
|
166
|
-
|
|
167
|
-
"]"
|
|
168
|
-
|
|
169
|
-
;
|
|
170
|
-
|
|
171
|
-
selector ::= ruleName | tokenType ;
|
|
172
|
-
|
|
173
|
-
ruleName ::= [name] | "*" ;
|
|
174
|
-
|
|
175
|
-
tokenType ::= "@"<NO_WHITESPACE>( [name] | "*" ) ;
|
|
176
|
-
|
|
177
|
-
startIndex ::= [number] ;
|
|
178
|
-
|
|
179
|
-
endIndex ::= [number] ;
|
|
180
|
-
|
|
181
|
-
index ::= [number] ;
|
|
182
|
-
|
|
183
|
-
unique ::= "!" ;`;
|
|
131
|
+
nonsense. ::= [type] | [keyword] | [primitive] | [query] | [special] | [name] | [number] | [unassigned] ;`;
|
|
184
132
|
|
|
185
133
|
export default bnf;
|
package/src/furtle/entries.js
CHANGED
|
@@ -20,13 +20,7 @@ const entries = [
|
|
|
20
20
|
"bracket": "^(?:\\{|\\})"
|
|
21
21
|
},
|
|
22
22
|
{
|
|
23
|
-
"special": "^(
|
|
24
|
-
},
|
|
25
|
-
{
|
|
26
|
-
"name": "^[a-zA-Z\-]+"
|
|
27
|
-
},
|
|
28
|
-
{
|
|
29
|
-
"number": "^[0-9]+"
|
|
23
|
+
"special": "^(?:!=|==|=|,|;|_|&&|\\|\\||\\(|\\))"
|
|
30
24
|
},
|
|
31
25
|
{
|
|
32
26
|
"unassigned": "^."
|
package/src/index.js
CHANGED
|
@@ -5,7 +5,6 @@ export { default as parsersUtilities } from "./utilities/parsers";
|
|
|
5
5
|
|
|
6
6
|
export { default as TeXLexer } from "./teX/lexer";
|
|
7
7
|
export { default as JSONLexer } from "./json/lexer";
|
|
8
|
-
export { default as FurtleLexer } from "./furtle/lexer";
|
|
9
8
|
export { default as NominalLexer } from "./nominal/lexer";
|
|
10
9
|
export { default as PlainTextLexer } from "./plainText/lexer";
|
|
11
10
|
export { default as CustomGrammarBNFLexer } from "./customGrammarBNF/lexer";
|
|
@@ -13,7 +12,6 @@ export { default as CustomGrammarPatternLexer } from "./customGrammarPattern/lex
|
|
|
13
12
|
|
|
14
13
|
export { default as TeXParser } from "./teX/parser";
|
|
15
14
|
export { default as JSONParser } from "./json/parser";
|
|
16
|
-
export { default as FurtleParser } from "./furtle/parser";
|
|
17
15
|
export { default as NominalParser } from "./nominal/parser";
|
|
18
16
|
export { default as PlainTextParser } from "./plainText/parser";
|
|
19
17
|
export { default as CustomGrammarBNFParser } from "./customGrammarBNF/parser";
|
package/src/utilities/lexers.js
CHANGED
|
@@ -4,19 +4,36 @@ import { lexerUtilities } from "occam-lexers";
|
|
|
4
4
|
|
|
5
5
|
import FurtleLexer from "../furtle/lexer";
|
|
6
6
|
|
|
7
|
+
import { augmentLexicalRules } from "../utilities/query";
|
|
8
|
+
|
|
7
9
|
const { lexerFromRules, rulesFromEntries } = lexerUtilities;
|
|
8
10
|
|
|
9
|
-
export function furtleLexerFromEntries(entries) {
|
|
10
|
-
|
|
11
|
-
|
|
11
|
+
export function furtleLexerFromEntries(entries, augmented = true) {
|
|
12
|
+
let rules;
|
|
13
|
+
|
|
14
|
+
rules = rulesFromEntries(entries);
|
|
15
|
+
|
|
16
|
+
if (augmented) {
|
|
17
|
+
rules = augmentRules(rules);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const furtleLexer = lexerFromRules(FurtleLexer, rules);
|
|
12
21
|
|
|
13
22
|
return furtleLexer;
|
|
14
23
|
}
|
|
15
24
|
|
|
16
|
-
export function furtleLexerFromNothing() {
|
|
17
|
-
const { entries } = FurtleLexer
|
|
18
|
-
|
|
19
|
-
|
|
25
|
+
export function furtleLexerFromNothing(augmented = true) {
|
|
26
|
+
const { entries } = FurtleLexer;
|
|
27
|
+
|
|
28
|
+
let rules;
|
|
29
|
+
|
|
30
|
+
rules = rulesFromEntries(entries);
|
|
31
|
+
|
|
32
|
+
if (augmented) {
|
|
33
|
+
rules = augmentRules(rules);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const furtleLexer = lexerFromRules(FurtleLexer, rules);
|
|
20
37
|
|
|
21
38
|
return furtleLexer;
|
|
22
39
|
}
|
|
@@ -25,3 +42,13 @@ export default {
|
|
|
25
42
|
furtleLexerFromEntries,
|
|
26
43
|
furtleLexerFromNothing
|
|
27
44
|
};
|
|
45
|
+
|
|
46
|
+
function augmentRules(rules) {
|
|
47
|
+
let lexicalRules = rules; ///
|
|
48
|
+
|
|
49
|
+
lexicalRules = augmentLexicalRules(lexicalRules);
|
|
50
|
+
|
|
51
|
+
rules = lexicalRules; ///
|
|
52
|
+
|
|
53
|
+
return rules;
|
|
54
|
+
}
|
package/src/utilities/parsers.js
CHANGED
|
@@ -5,21 +5,27 @@ import { eliminateLeftRecursion } from "occam-grammar-utilities";
|
|
|
5
5
|
|
|
6
6
|
import FurtleParser from "../furtle/parser";
|
|
7
7
|
|
|
8
|
+
import { augmentBNFRules } from "../utilities/query";
|
|
9
|
+
|
|
8
10
|
const { rulesFromBNF, parserFromRules, parserFromRulesAndStartRuleName } = parserUtilities;
|
|
9
11
|
|
|
10
|
-
export function furtleParserFromBNF(bnf) {
|
|
12
|
+
export function furtleParserFromBNF(bnf, augmented = true) {
|
|
11
13
|
let rules;
|
|
12
14
|
|
|
13
15
|
rules = rulesFromBNF(bnf);
|
|
14
16
|
|
|
15
17
|
rules = eliminateLeftRecursion(rules); ///
|
|
16
18
|
|
|
19
|
+
if (augmented) {
|
|
20
|
+
rules = augmentRules(rules);
|
|
21
|
+
}
|
|
22
|
+
|
|
17
23
|
const furtleParser = parserFromRules(FurtleParser, rules);
|
|
18
24
|
|
|
19
25
|
return furtleParser;
|
|
20
26
|
}
|
|
21
27
|
|
|
22
|
-
export function furtleParserFromNothing() {
|
|
28
|
+
export function furtleParserFromNothing(augmented = true) {
|
|
23
29
|
const { bnf } = FurtleParser;
|
|
24
30
|
|
|
25
31
|
let rules;
|
|
@@ -28,18 +34,26 @@ export function furtleParserFromNothing() {
|
|
|
28
34
|
|
|
29
35
|
rules = eliminateLeftRecursion(rules); ///
|
|
30
36
|
|
|
37
|
+
if (augmented) {
|
|
38
|
+
rules = augmentRules(rules);
|
|
39
|
+
}
|
|
40
|
+
|
|
31
41
|
const furtleParser = parserFromRules(FurtleParser, rules);
|
|
32
42
|
|
|
33
43
|
return furtleParser;
|
|
34
44
|
}
|
|
35
45
|
|
|
36
|
-
export function furtleParserFromBNFAndStartRuleName(bnf, startRuleName) {
|
|
46
|
+
export function furtleParserFromBNFAndStartRuleName(bnf, startRuleName, augmented = true) {
|
|
37
47
|
let rules;
|
|
38
48
|
|
|
39
49
|
rules = rulesFromBNF(bnf);
|
|
40
50
|
|
|
41
51
|
rules = eliminateLeftRecursion(rules); ///
|
|
42
52
|
|
|
53
|
+
if (augmented) {
|
|
54
|
+
rules = augmentRules(rules);
|
|
55
|
+
}
|
|
56
|
+
|
|
43
57
|
const furtleParser = parserFromRulesAndStartRuleName(FurtleParser, rules, startRuleName);
|
|
44
58
|
|
|
45
59
|
return furtleParser;
|
|
@@ -50,3 +64,13 @@ export default {
|
|
|
50
64
|
furtleParserFromNothing,
|
|
51
65
|
furtleParserFromBNFAndStartRuleName
|
|
52
66
|
};
|
|
67
|
+
|
|
68
|
+
function augmentRules(rules) {
|
|
69
|
+
let bnfRules = rules; ///
|
|
70
|
+
|
|
71
|
+
bnfRules = augmentBNFRules(bnfRules);
|
|
72
|
+
|
|
73
|
+
rules = bnfRules; ///
|
|
74
|
+
|
|
75
|
+
return rules;
|
|
76
|
+
}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
import { Rule } from "occam-parsers";
|
|
4
|
+
import { arrayUtilities } from "necessary";
|
|
5
|
+
import { ExpressionLexer, ExpressionParser } from "occam-query";
|
|
6
|
+
|
|
7
|
+
import { ERROR_RULE_NAME, EXPRESSION_RULE_NAME } from "../constants";
|
|
8
|
+
|
|
9
|
+
const { first, extract } = arrayUtilities;
|
|
10
|
+
|
|
11
|
+
const expressionBNFRules = expressionBNFRulesFromNothing(),
|
|
12
|
+
expressionLexicalRules = expressionLexicalRulesFromNothing();
|
|
13
|
+
|
|
14
|
+
export function augmentLexicalRules(lexicalRules) {
|
|
15
|
+
lexicalRules = [ ///
|
|
16
|
+
...lexicalRules
|
|
17
|
+
];
|
|
18
|
+
|
|
19
|
+
const unassignedLexicalRule = lexicalRules.pop(); ///
|
|
20
|
+
|
|
21
|
+
lexicalRules = [ ///
|
|
22
|
+
...lexicalRules,
|
|
23
|
+
...expressionLexicalRules
|
|
24
|
+
];
|
|
25
|
+
|
|
26
|
+
return lexicalRules;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function augmentBNFRules(bnrRules) {
|
|
30
|
+
bnrRules = [ ///
|
|
31
|
+
...bnrRules,
|
|
32
|
+
...expressionBNFRules
|
|
33
|
+
];
|
|
34
|
+
|
|
35
|
+
return bnrRules;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function expressionLexicalRulesFromNothing() {
|
|
39
|
+
const expressionLexer = ExpressionLexer.fromNothing(),
|
|
40
|
+
rules = expressionLexer.getRules(),
|
|
41
|
+
expressionLexicalRules = [ ///
|
|
42
|
+
...rules
|
|
43
|
+
];
|
|
44
|
+
|
|
45
|
+
return expressionLexicalRules;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function expressionBNFRulesFromNothing() {
|
|
49
|
+
const expressionParser = ExpressionParser.fromNothing(),
|
|
50
|
+
ruleMap = expressionParser.getRuleMap(),
|
|
51
|
+
rules = Object.values(ruleMap);
|
|
52
|
+
|
|
53
|
+
extract(rules, (rule) => {
|
|
54
|
+
const ruleName = rule.getName();
|
|
55
|
+
|
|
56
|
+
if (ruleName === ERROR_RULE_NAME) {
|
|
57
|
+
return true;
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
const expressionRule = extract(rules, (rule) => {
|
|
62
|
+
const ruleName = rule.getName();
|
|
63
|
+
|
|
64
|
+
if (ruleName === EXPRESSION_RULE_NAME) {
|
|
65
|
+
return true;
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
let rule = expressionRule, ///
|
|
70
|
+
definitions = rule.getDefinitions();
|
|
71
|
+
|
|
72
|
+
const name = rule.getName(),
|
|
73
|
+
opacity = rule.getOpacity(),
|
|
74
|
+
NonTerminalNode = rule.getNonTerminalNode(),
|
|
75
|
+
firstDefinition = first(definitions),
|
|
76
|
+
definition = firstDefinition; ///
|
|
77
|
+
|
|
78
|
+
definitions = [
|
|
79
|
+
definition
|
|
80
|
+
];
|
|
81
|
+
|
|
82
|
+
rule = Rule.fromNameOpacityDefinitionsAndNonTerminalNode(name, opacity, definitions, NonTerminalNode);
|
|
83
|
+
|
|
84
|
+
rules.unshift(rule);
|
|
85
|
+
|
|
86
|
+
const expressionBNFRules = rules; ///
|
|
87
|
+
|
|
88
|
+
return expressionBNFRules;
|
|
89
|
+
}
|