occam-custom-grammars 5.0.1228 → 5.0.1230

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.
@@ -0,0 +1,144 @@
1
+ "use strict";
2
+
3
+ import { arrayUtilities } from "necessary";
4
+
5
+ import typesMap from "../typesMap";
6
+
7
+ import { nominalLexer } from "../utilities/nominal";
8
+ import { UNDERSCORE_CHARACTER } from "../constants";
9
+ import { nodeQuery, nodesQuery } from "../utilities/query";
10
+ import { customGrammarBNFLexer, customGrammarBNFParser, customGrammarVocabularyLexer, customGrammarVocabularyParser } from "../utilities/grammar";
11
+
12
+ const { first, second } = arrayUtilities;
13
+
14
+ const expressionNodesQuery = nodesQuery("//expression"),
15
+ ruleNameTerminalNodeQuery = nodeQuery("/document/rule/name/@*!"),
16
+ stringLiteralTerminalNodesQuery = nodesQuery("//stringLiteral/@*!"),
17
+ significantTokenTypeTerminalNodesQuery = nodesQuery("//significantTokenType/@*!");
18
+
19
+ export function validateBNF(bnf, ruleName) {
20
+ const content = bnf,
21
+ tokens = customGrammarBNFLexer.tokenise(content),
22
+ node = customGrammarBNFParser.parse(tokens);
23
+
24
+ if (node === null) {
25
+ return;
26
+ }
27
+
28
+ const ruleNameTerminalNode = ruleNameTerminalNodeQuery(node);
29
+
30
+ if (ruleNameTerminalNode !== null) {
31
+ const name = nameFromRuleNameTerminalNode(ruleNameTerminalNode);
32
+
33
+ if (name !== ruleName) {
34
+ throw new Error(`The '${name}' rule should be named '${ruleName}'.`);
35
+ }
36
+ }
37
+
38
+ const types = typesMap[ruleName],
39
+ significantTokenTypeTerminalNodes = significantTokenTypeTerminalNodesQuery(node);
40
+
41
+ significantTokenTypeTerminalNodes.forEach((significantTokenTypeTerminalNode) => {
42
+ const type = typeFromSignificantTokenTypeTerminalNode(significantTokenTypeTerminalNode),
43
+ typesIncludeType = types.includes(type);
44
+
45
+ if (!typesIncludeType) {
46
+ throw new Error(`The '${type}' type is not included in the '${ruleName}' rule's types.`)
47
+ }
48
+ });
49
+
50
+ const stringLiteralTerminalNodes = stringLiteralTerminalNodesQuery(node);
51
+
52
+ stringLiteralTerminalNodes.forEach((stringLiteralTerminalNode) => {
53
+ const content = contentFromStringLiteralTerminalNode(stringLiteralTerminalNode);
54
+
55
+ if (content === UNDERSCORE_CHARACTER) {
56
+ throw new Error(`The "${content}" string literal cannot be an underscore.`);
57
+ }
58
+
59
+ const tokens = nominalLexer.tokenise(content),
60
+ tokensLength = tokens.length;
61
+
62
+ if (tokensLength !== 1) {
63
+ throw new Error(`Tokenising the "${content}" string literal does not result in a single token.`);
64
+ }
65
+
66
+ const firstToken = first(tokens),
67
+ token = firstToken, ///
68
+ type = token.getType(),
69
+ typesIncludeType = types.includes(type);
70
+
71
+ if (!typesIncludeType) {
72
+ throw new Error(`The "${content}" string literal's token's '${type}' type is not included in the '${ruleName}' rule's types.`)
73
+ }
74
+ });
75
+ }
76
+
77
+ export function validateVocabulary(vocabulary) {
78
+ const content = vocabulary, ///
79
+ tokens = customGrammarVocabularyLexer.tokenise(content),
80
+ node = customGrammarVocabularyParser.parse(tokens);
81
+
82
+ if (node === null) {
83
+ return;
84
+ }
85
+
86
+ const expressionNodes = expressionNodesQuery(node);
87
+
88
+ expressionNodes.forEach((expressionNode) => {
89
+ const content = contentFromExpressionNode(expressionNode),
90
+ tokens = nominalLexer.tokenise(content),
91
+ tokensLength = tokens.length;
92
+
93
+ if (tokensLength > 1) {
94
+ throw new Error(`Tokenising the '${content}' content results in more than one token.`);
95
+ }
96
+
97
+ const firstToken = first(tokens),
98
+ token = firstToken,
99
+ type = token.getType();
100
+
101
+ if (type !== UNASSIGNED_TYPE) {
102
+ throw new Error(`The '${type}' type of the '${content}' token is not 'unassigned'.`);
103
+ }
104
+
105
+ if (content === UNDERSCORE_CHARACTER) {
106
+ throw new Error(`The '${content}' token cannot be an underscore.`);
107
+ }
108
+ });
109
+ }
110
+
111
+ function nameFromRuleNameTerminalNode(ruleNameTerminalNode) {
112
+ let name;
113
+
114
+ const content = ruleNameTerminalNode.getContent();
115
+
116
+ name = content; ///
117
+
118
+ return name;
119
+ }
120
+
121
+ function contentFromStringLiteralTerminalNode(stringLiteralTerminalNode) {
122
+ let content;
123
+
124
+ content = stringLiteralTerminalNode.getContent();
125
+
126
+ const matches = content.match(/"([^"]*)"/),
127
+ secondMatch = second(matches);
128
+
129
+ content = secondMatch; ///
130
+
131
+ return content;
132
+ }
133
+
134
+ function typeFromSignificantTokenTypeTerminalNode(significantTokenTypeTerminalNode) {
135
+ let type;
136
+
137
+ const content = significantTokenTypeTerminalNode.getContent(),
138
+ matches = content.match(/\[([^\]]*)\]/),
139
+ secondMatch = second(matches);
140
+
141
+ type = secondMatch; ///
142
+
143
+ return type;
144
+ }
@@ -1,76 +1,24 @@
1
1
  "use strict";
2
2
 
3
- import { CommonLexer } from "occam-lexers";
4
- import { NominalLexer } from "occam-grammars";
5
3
  import { arrayUtilities } from "necessary";
6
- import { CustomGrammarVocabularyLexer, CustomGrammarVocabularyParser } from "occam-grammars";
7
4
 
8
5
  import { nodesQuery } from "../utilities/query";
9
- import { EMPTY_STRING, UNASSIGNED_TYPE, UNDERSCORE_CHARACTER } from "../constants";
6
+ import { customGrammarVocabularyLexer, customGrammarVocabularyParser } from "../utilities/grammar"
10
7
 
11
- const errorNodesQuery = nodesQuery("//error"),
12
- expressionNodesQuery = nodesQuery("//expression")
8
+ const { second } = arrayUtilities;
13
9
 
14
- const nominalLexer = CommonLexer.fromNothing(NominalLexer),
15
- customGrammarVocabularyLexer = CustomGrammarVocabularyLexer.fromNothing(),
16
- customGrammarVocabularyParser = CustomGrammarVocabularyParser.fromNothing();
17
-
18
- const { first, second } = arrayUtilities;
19
-
20
- export function validateVocabulary(vocabulary) {
21
- if ((vocabulary === null) || (vocabulary === EMPTY_STRING)) {
22
- return;
23
- }
10
+ const expressionNodesQuery = nodesQuery("//expression");
24
11
 
12
+ export function expressionsFromVocabulary(vocabulary, expressions) {
25
13
  const content = vocabulary, ///
26
14
  tokens = customGrammarVocabularyLexer.tokenise(content),
27
15
  node = customGrammarVocabularyParser.parse(tokens);
28
16
 
29
17
  if (node === null) {
30
- throw new Error("The vocabulary cannot be parsed.");
31
- }
32
-
33
- const errorNodes = errorNodesQuery(node),
34
- errorNodesLength = errorNodes.length;
35
-
36
- if (errorNodesLength > 0) {
37
- throw new Error("The vocabulary contains errors.");
38
- }
39
-
40
- const expressionNodes = expressionNodesQuery(node);
41
-
42
- expressionNodes.forEach((expressionNode) => {
43
- const content = contentFromExpressionNode(expressionNode),
44
- tokens = nominalLexer.tokenise(content),
45
- tokensLength = tokens.length;
46
-
47
- if (tokensLength > 1) {
48
- throw new Error(`Tokenising '${content}' results in more than one token.`);
49
- }
50
-
51
- const firstToken = first(tokens),
52
- token = firstToken,
53
- type = token.getType();
54
-
55
- if (type !== UNASSIGNED_TYPE) {
56
- throw new Error(`The '${type}' type of the '${content}' token is not 'unassigned'.`);
57
- }
58
-
59
- if (content === UNDERSCORE_CHARACTER) {
60
- throw new Error(`The '${content}' token cannot be an underscore.`);
61
- }
62
- });
63
- }
64
-
65
- export function expressionsFromVocabulary(vocabulary, expressions) {
66
- if ((vocabulary === null) || (vocabulary === EMPTY_STRING)) {
67
18
  return;
68
19
  }
69
20
 
70
- const content = vocabulary, ///
71
- tokens = customGrammarVocabularyLexer.tokenise(content),
72
- node = customGrammarVocabularyParser.parse(tokens),
73
- expressionNodes = expressionNodesQuery(node);
21
+ const expressionNodes = expressionNodesQuery(node);
74
22
 
75
23
  expressionNodes.forEach((expressionNode) => {
76
24
  const content = contentFromExpressionNode(expressionNode),
@@ -1,18 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", {
3
- value: true
4
- });
5
- Object.defineProperty(exports, "validateBNf", {
6
- enumerable: true,
7
- get: function() {
8
- return validateBNf;
9
- }
10
- });
11
- var _constants = require("../constants");
12
- function validateBNf(buf) {
13
- if (buf === null || buf === _constants.EMPTY_STRING) {
14
- return;
15
- }
16
- }
17
-
18
- //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy91dGlsaXRpZXMvYm5mLmpzIl0sInNvdXJjZXNDb250ZW50IjpbIlwidXNlIHN0cmljdFwiO1xuXG5cbmltcG9ydCB7IEVNUFRZX1NUUklORyB9IGZyb20gXCIuLi9jb25zdGFudHNcIjtcblxuZXhwb3J0IGZ1bmN0aW9uIHZhbGlkYXRlQk5mKGJ1Zikge1xuICBpZiAoKGJ1ZiA9PT0gbnVsbCkgfHwgKGJ1ZiA9PT0gRU1QVFlfU1RSSU5HKSkge1xuICAgIHJldHVybjtcbiAgfVxuXG5cbn1cbiJdLCJuYW1lcyI6WyJ2YWxpZGF0ZUJOZiIsImJ1ZiIsIkVNUFRZX1NUUklORyJdLCJtYXBwaW5ncyI6IkFBQUE7Ozs7K0JBS2dCQTs7O2VBQUFBOzs7eUJBRmE7QUFFdEIsU0FBU0EsWUFBWUMsR0FBRztJQUM3QixJQUFJLEFBQUNBLFFBQVEsUUFBVUEsUUFBUUMsdUJBQVksRUFBRztRQUM1QztJQUNGO0FBR0YifQ==
@@ -1,12 +0,0 @@
1
- "use strict";
2
-
3
-
4
- import { EMPTY_STRING } from "../constants";
5
-
6
- export function validateBNf(buf) {
7
- if ((buf === null) || (buf === EMPTY_STRING)) {
8
- return;
9
- }
10
-
11
-
12
- }