@lokascript/semantic 1.0.0
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/LICENSE +21 -0
- package/README.md +686 -0
- package/dist/browser-ar.ar.global.js +2 -0
- package/dist/browser-core.core.global.js +2 -0
- package/dist/browser-de.de.global.js +2 -0
- package/dist/browser-east-asian.east-asian.global.js +2 -0
- package/dist/browser-en-tr.en-tr.global.js +2 -0
- package/dist/browser-en.en.global.js +2 -0
- package/dist/browser-es-en.es-en.global.js +2 -0
- package/dist/browser-es.es.global.js +2 -0
- package/dist/browser-fr.fr.global.js +2 -0
- package/dist/browser-id.id.global.js +2 -0
- package/dist/browser-ja.ja.global.js +2 -0
- package/dist/browser-ko.ko.global.js +2 -0
- package/dist/browser-lazy.lazy.global.js +2 -0
- package/dist/browser-priority.priority.global.js +2 -0
- package/dist/browser-pt.pt.global.js +2 -0
- package/dist/browser-qu.qu.global.js +2 -0
- package/dist/browser-sw.sw.global.js +2 -0
- package/dist/browser-tr.tr.global.js +2 -0
- package/dist/browser-western.western.global.js +2 -0
- package/dist/browser-zh.zh.global.js +2 -0
- package/dist/browser.global.js +3 -0
- package/dist/browser.global.js.map +1 -0
- package/dist/index.cjs +35051 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +3426 -0
- package/dist/index.d.ts +3426 -0
- package/dist/index.js +34890 -0
- package/dist/index.js.map +1 -0
- package/dist/languages/ar.d.ts +78 -0
- package/dist/languages/ar.js +1622 -0
- package/dist/languages/ar.js.map +1 -0
- package/dist/languages/de.d.ts +38 -0
- package/dist/languages/de.js +1168 -0
- package/dist/languages/de.js.map +1 -0
- package/dist/languages/en.d.ts +44 -0
- package/dist/languages/en.js +3491 -0
- package/dist/languages/en.js.map +1 -0
- package/dist/languages/es.d.ts +52 -0
- package/dist/languages/es.js +1493 -0
- package/dist/languages/es.js.map +1 -0
- package/dist/languages/fr.d.ts +37 -0
- package/dist/languages/fr.js +1159 -0
- package/dist/languages/fr.js.map +1 -0
- package/dist/languages/id.d.ts +35 -0
- package/dist/languages/id.js +1152 -0
- package/dist/languages/id.js.map +1 -0
- package/dist/languages/ja.d.ts +53 -0
- package/dist/languages/ja.js +1430 -0
- package/dist/languages/ja.js.map +1 -0
- package/dist/languages/ko.d.ts +51 -0
- package/dist/languages/ko.js +1729 -0
- package/dist/languages/ko.js.map +1 -0
- package/dist/languages/pt.d.ts +37 -0
- package/dist/languages/pt.js +1127 -0
- package/dist/languages/pt.js.map +1 -0
- package/dist/languages/qu.d.ts +36 -0
- package/dist/languages/qu.js +1143 -0
- package/dist/languages/qu.js.map +1 -0
- package/dist/languages/sw.d.ts +35 -0
- package/dist/languages/sw.js +1147 -0
- package/dist/languages/sw.js.map +1 -0
- package/dist/languages/tr.d.ts +45 -0
- package/dist/languages/tr.js +1529 -0
- package/dist/languages/tr.js.map +1 -0
- package/dist/languages/zh.d.ts +58 -0
- package/dist/languages/zh.js +1257 -0
- package/dist/languages/zh.js.map +1 -0
- package/dist/types-C4dcj53L.d.ts +600 -0
- package/package.json +202 -0
- package/src/__test-utils__/index.ts +7 -0
- package/src/__test-utils__/test-helpers.ts +8 -0
- package/src/__types__/test-helpers.ts +122 -0
- package/src/analysis/index.ts +479 -0
- package/src/ast-builder/command-mappers.ts +1133 -0
- package/src/ast-builder/expression-parser/index.ts +41 -0
- package/src/ast-builder/expression-parser/parser.ts +563 -0
- package/src/ast-builder/expression-parser/tokenizer.ts +394 -0
- package/src/ast-builder/expression-parser/types.ts +208 -0
- package/src/ast-builder/index.ts +536 -0
- package/src/ast-builder/value-converters.ts +172 -0
- package/src/bridge.ts +275 -0
- package/src/browser-ar.ts +162 -0
- package/src/browser-core.ts +231 -0
- package/src/browser-de.ts +162 -0
- package/src/browser-east-asian.ts +173 -0
- package/src/browser-en-tr.ts +165 -0
- package/src/browser-en.ts +157 -0
- package/src/browser-es-en.ts +200 -0
- package/src/browser-es.ts +170 -0
- package/src/browser-fr.ts +162 -0
- package/src/browser-id.ts +162 -0
- package/src/browser-ja.ts +162 -0
- package/src/browser-ko.ts +162 -0
- package/src/browser-lazy.ts +189 -0
- package/src/browser-priority.ts +214 -0
- package/src/browser-pt.ts +162 -0
- package/src/browser-qu.ts +162 -0
- package/src/browser-sw.ts +162 -0
- package/src/browser-tr.ts +162 -0
- package/src/browser-western.ts +181 -0
- package/src/browser-zh.ts +162 -0
- package/src/browser.ts +268 -0
- package/src/cache/index.ts +14 -0
- package/src/cache/semantic-cache.ts +344 -0
- package/src/core-bridge.ts +372 -0
- package/src/explicit/converter.ts +258 -0
- package/src/explicit/index.ts +18 -0
- package/src/explicit/parser.ts +236 -0
- package/src/explicit/renderer.ts +424 -0
- package/src/generators/command-schemas.ts +1636 -0
- package/src/generators/event-handler-generator.ts +109 -0
- package/src/generators/index.ts +117 -0
- package/src/generators/language-profiles.ts +139 -0
- package/src/generators/pattern-generator.ts +537 -0
- package/src/generators/profiles/arabic.ts +131 -0
- package/src/generators/profiles/bengali.ts +132 -0
- package/src/generators/profiles/chinese.ts +124 -0
- package/src/generators/profiles/english.ts +113 -0
- package/src/generators/profiles/french.ts +125 -0
- package/src/generators/profiles/german.ts +126 -0
- package/src/generators/profiles/hindi.ts +146 -0
- package/src/generators/profiles/index.ts +46 -0
- package/src/generators/profiles/indonesian.ts +125 -0
- package/src/generators/profiles/italian.ts +139 -0
- package/src/generators/profiles/japanese.ts +149 -0
- package/src/generators/profiles/korean.ts +127 -0
- package/src/generators/profiles/marker-templates.ts +288 -0
- package/src/generators/profiles/ms.ts +130 -0
- package/src/generators/profiles/polish.ts +249 -0
- package/src/generators/profiles/portuguese.ts +115 -0
- package/src/generators/profiles/quechua.ts +113 -0
- package/src/generators/profiles/russian.ts +260 -0
- package/src/generators/profiles/spanish.ts +130 -0
- package/src/generators/profiles/swahili.ts +129 -0
- package/src/generators/profiles/thai.ts +132 -0
- package/src/generators/profiles/tl.ts +128 -0
- package/src/generators/profiles/turkish.ts +124 -0
- package/src/generators/profiles/types.ts +165 -0
- package/src/generators/profiles/ukrainian.ts +270 -0
- package/src/generators/profiles/vietnamese.ts +133 -0
- package/src/generators/schema-error-codes.ts +160 -0
- package/src/generators/schema-validator.ts +391 -0
- package/src/index.ts +429 -0
- package/src/language-building-schema.ts +3170 -0
- package/src/language-loader.ts +394 -0
- package/src/languages/_all.ts +65 -0
- package/src/languages/ar.ts +15 -0
- package/src/languages/bn.ts +16 -0
- package/src/languages/de.ts +15 -0
- package/src/languages/en.ts +29 -0
- package/src/languages/es.ts +15 -0
- package/src/languages/fr.ts +15 -0
- package/src/languages/hi.ts +26 -0
- package/src/languages/id.ts +15 -0
- package/src/languages/index.ts +18 -0
- package/src/languages/it.ts +15 -0
- package/src/languages/ja.ts +15 -0
- package/src/languages/ko.ts +15 -0
- package/src/languages/ms.ts +16 -0
- package/src/languages/pl.ts +18 -0
- package/src/languages/pt.ts +15 -0
- package/src/languages/qu.ts +15 -0
- package/src/languages/ru.ts +26 -0
- package/src/languages/sw.ts +15 -0
- package/src/languages/th.ts +16 -0
- package/src/languages/tl.ts +16 -0
- package/src/languages/tr.ts +15 -0
- package/src/languages/uk.ts +26 -0
- package/src/languages/vi.ts +16 -0
- package/src/languages/zh.ts +15 -0
- package/src/parser/index.ts +15 -0
- package/src/parser/pattern-matcher.ts +1181 -0
- package/src/parser/semantic-parser.ts +573 -0
- package/src/parser/utils/index.ts +35 -0
- package/src/parser/utils/marker-resolution.ts +111 -0
- package/src/parser/utils/possessive-keywords.ts +43 -0
- package/src/parser/utils/role-positioning.ts +70 -0
- package/src/parser/utils/type-validation.ts +134 -0
- package/src/patterns/add/ar.ts +71 -0
- package/src/patterns/add/bn.ts +70 -0
- package/src/patterns/add/hi.ts +69 -0
- package/src/patterns/add/index.ts +87 -0
- package/src/patterns/add/it.ts +61 -0
- package/src/patterns/add/ja.ts +93 -0
- package/src/patterns/add/ko.ts +74 -0
- package/src/patterns/add/ms.ts +30 -0
- package/src/patterns/add/pl.ts +62 -0
- package/src/patterns/add/ru.ts +62 -0
- package/src/patterns/add/th.ts +49 -0
- package/src/patterns/add/tl.ts +30 -0
- package/src/patterns/add/tr.ts +71 -0
- package/src/patterns/add/uk.ts +62 -0
- package/src/patterns/add/vi.ts +61 -0
- package/src/patterns/add/zh.ts +71 -0
- package/src/patterns/builders.ts +207 -0
- package/src/patterns/decrement/bn.ts +70 -0
- package/src/patterns/decrement/de.ts +42 -0
- package/src/patterns/decrement/hi.ts +68 -0
- package/src/patterns/decrement/index.ts +79 -0
- package/src/patterns/decrement/it.ts +69 -0
- package/src/patterns/decrement/ms.ts +30 -0
- package/src/patterns/decrement/pl.ts +58 -0
- package/src/patterns/decrement/ru.ts +58 -0
- package/src/patterns/decrement/th.ts +49 -0
- package/src/patterns/decrement/tl.ts +30 -0
- package/src/patterns/decrement/tr.ts +48 -0
- package/src/patterns/decrement/uk.ts +58 -0
- package/src/patterns/decrement/vi.ts +61 -0
- package/src/patterns/decrement/zh.ts +32 -0
- package/src/patterns/en.ts +302 -0
- package/src/patterns/event-handler/ar.ts +151 -0
- package/src/patterns/event-handler/bn.ts +72 -0
- package/src/patterns/event-handler/de.ts +117 -0
- package/src/patterns/event-handler/en.ts +117 -0
- package/src/patterns/event-handler/es.ts +136 -0
- package/src/patterns/event-handler/fr.ts +117 -0
- package/src/patterns/event-handler/hi.ts +64 -0
- package/src/patterns/event-handler/id.ts +117 -0
- package/src/patterns/event-handler/index.ts +119 -0
- package/src/patterns/event-handler/it.ts +54 -0
- package/src/patterns/event-handler/ja.ts +118 -0
- package/src/patterns/event-handler/ko.ts +133 -0
- package/src/patterns/event-handler/ms.ts +30 -0
- package/src/patterns/event-handler/pl.ts +62 -0
- package/src/patterns/event-handler/pt.ts +117 -0
- package/src/patterns/event-handler/qu.ts +66 -0
- package/src/patterns/event-handler/ru.ts +62 -0
- package/src/patterns/event-handler/shared.ts +270 -0
- package/src/patterns/event-handler/sw.ts +117 -0
- package/src/patterns/event-handler/th.ts +53 -0
- package/src/patterns/event-handler/tl.ts +30 -0
- package/src/patterns/event-handler/tr.ts +170 -0
- package/src/patterns/event-handler/uk.ts +62 -0
- package/src/patterns/event-handler/vi.ts +61 -0
- package/src/patterns/event-handler/zh.ts +150 -0
- package/src/patterns/get/ar.ts +49 -0
- package/src/patterns/get/bn.ts +47 -0
- package/src/patterns/get/de.ts +32 -0
- package/src/patterns/get/hi.ts +52 -0
- package/src/patterns/get/index.ts +83 -0
- package/src/patterns/get/it.ts +56 -0
- package/src/patterns/get/ja.ts +53 -0
- package/src/patterns/get/ko.ts +53 -0
- package/src/patterns/get/ms.ts +30 -0
- package/src/patterns/get/pl.ts +57 -0
- package/src/patterns/get/ru.ts +57 -0
- package/src/patterns/get/th.ts +29 -0
- package/src/patterns/get/tl.ts +30 -0
- package/src/patterns/get/uk.ts +57 -0
- package/src/patterns/get/vi.ts +48 -0
- package/src/patterns/grammar-transformed/index.ts +39 -0
- package/src/patterns/grammar-transformed/ja.ts +1713 -0
- package/src/patterns/grammar-transformed/ko.ts +1311 -0
- package/src/patterns/grammar-transformed/tr.ts +1067 -0
- package/src/patterns/hide/ar.ts +67 -0
- package/src/patterns/hide/bn.ts +47 -0
- package/src/patterns/hide/de.ts +36 -0
- package/src/patterns/hide/hi.ts +61 -0
- package/src/patterns/hide/index.ts +91 -0
- package/src/patterns/hide/it.ts +56 -0
- package/src/patterns/hide/ja.ts +69 -0
- package/src/patterns/hide/ko.ts +69 -0
- package/src/patterns/hide/ms.ts +30 -0
- package/src/patterns/hide/pl.ts +57 -0
- package/src/patterns/hide/ru.ts +57 -0
- package/src/patterns/hide/th.ts +29 -0
- package/src/patterns/hide/tl.ts +30 -0
- package/src/patterns/hide/tr.ts +65 -0
- package/src/patterns/hide/uk.ts +57 -0
- package/src/patterns/hide/vi.ts +56 -0
- package/src/patterns/hide/zh.ts +68 -0
- package/src/patterns/increment/bn.ts +70 -0
- package/src/patterns/increment/de.ts +36 -0
- package/src/patterns/increment/hi.ts +68 -0
- package/src/patterns/increment/index.ts +79 -0
- package/src/patterns/increment/it.ts +69 -0
- package/src/patterns/increment/ms.ts +30 -0
- package/src/patterns/increment/pl.ts +58 -0
- package/src/patterns/increment/ru.ts +58 -0
- package/src/patterns/increment/th.ts +49 -0
- package/src/patterns/increment/tl.ts +30 -0
- package/src/patterns/increment/tr.ts +52 -0
- package/src/patterns/increment/uk.ts +58 -0
- package/src/patterns/increment/vi.ts +61 -0
- package/src/patterns/increment/zh.ts +32 -0
- package/src/patterns/index.ts +84 -0
- package/src/patterns/languages/en/control-flow.ts +93 -0
- package/src/patterns/languages/en/fetch.ts +62 -0
- package/src/patterns/languages/en/index.ts +42 -0
- package/src/patterns/languages/en/repeat.ts +67 -0
- package/src/patterns/languages/en/set.ts +48 -0
- package/src/patterns/languages/en/swap.ts +38 -0
- package/src/patterns/languages/en/temporal.ts +57 -0
- package/src/patterns/put/ar.ts +74 -0
- package/src/patterns/put/bn.ts +53 -0
- package/src/patterns/put/en.ts +74 -0
- package/src/patterns/put/es.ts +74 -0
- package/src/patterns/put/hi.ts +69 -0
- package/src/patterns/put/id.ts +96 -0
- package/src/patterns/put/index.ts +99 -0
- package/src/patterns/put/it.ts +56 -0
- package/src/patterns/put/ja.ts +75 -0
- package/src/patterns/put/ko.ts +67 -0
- package/src/patterns/put/ms.ts +30 -0
- package/src/patterns/put/pl.ts +81 -0
- package/src/patterns/put/ru.ts +85 -0
- package/src/patterns/put/th.ts +32 -0
- package/src/patterns/put/tl.ts +30 -0
- package/src/patterns/put/tr.ts +67 -0
- package/src/patterns/put/uk.ts +85 -0
- package/src/patterns/put/vi.ts +72 -0
- package/src/patterns/put/zh.ts +62 -0
- package/src/patterns/registry.ts +163 -0
- package/src/patterns/remove/ar.ts +71 -0
- package/src/patterns/remove/bn.ts +68 -0
- package/src/patterns/remove/hi.ts +69 -0
- package/src/patterns/remove/index.ts +87 -0
- package/src/patterns/remove/it.ts +69 -0
- package/src/patterns/remove/ja.ts +74 -0
- package/src/patterns/remove/ko.ts +78 -0
- package/src/patterns/remove/ms.ts +30 -0
- package/src/patterns/remove/pl.ts +62 -0
- package/src/patterns/remove/ru.ts +62 -0
- package/src/patterns/remove/th.ts +49 -0
- package/src/patterns/remove/tl.ts +30 -0
- package/src/patterns/remove/tr.ts +78 -0
- package/src/patterns/remove/uk.ts +62 -0
- package/src/patterns/remove/vi.ts +61 -0
- package/src/patterns/remove/zh.ts +72 -0
- package/src/patterns/set/ar.ts +84 -0
- package/src/patterns/set/bn.ts +53 -0
- package/src/patterns/set/de.ts +84 -0
- package/src/patterns/set/es.ts +92 -0
- package/src/patterns/set/fr.ts +88 -0
- package/src/patterns/set/hi.ts +56 -0
- package/src/patterns/set/id.ts +84 -0
- package/src/patterns/set/index.ts +107 -0
- package/src/patterns/set/it.ts +56 -0
- package/src/patterns/set/ja.ts +86 -0
- package/src/patterns/set/ko.ts +85 -0
- package/src/patterns/set/ms.ts +30 -0
- package/src/patterns/set/pl.ts +57 -0
- package/src/patterns/set/pt.ts +84 -0
- package/src/patterns/set/ru.ts +57 -0
- package/src/patterns/set/th.ts +31 -0
- package/src/patterns/set/tl.ts +30 -0
- package/src/patterns/set/tr.ts +107 -0
- package/src/patterns/set/uk.ts +57 -0
- package/src/patterns/set/vi.ts +53 -0
- package/src/patterns/set/zh.ts +84 -0
- package/src/patterns/show/ar.ts +67 -0
- package/src/patterns/show/bn.ts +47 -0
- package/src/patterns/show/de.ts +32 -0
- package/src/patterns/show/fr.ts +32 -0
- package/src/patterns/show/hi.ts +61 -0
- package/src/patterns/show/index.ts +95 -0
- package/src/patterns/show/it.ts +56 -0
- package/src/patterns/show/ja.ts +69 -0
- package/src/patterns/show/ko.ts +73 -0
- package/src/patterns/show/ms.ts +30 -0
- package/src/patterns/show/pl.ts +57 -0
- package/src/patterns/show/ru.ts +57 -0
- package/src/patterns/show/th.ts +29 -0
- package/src/patterns/show/tl.ts +30 -0
- package/src/patterns/show/tr.ts +65 -0
- package/src/patterns/show/uk.ts +57 -0
- package/src/patterns/show/vi.ts +56 -0
- package/src/patterns/show/zh.ts +68 -0
- package/src/patterns/take/ar.ts +51 -0
- package/src/patterns/take/index.ts +31 -0
- package/src/patterns/toggle/ar.ts +61 -0
- package/src/patterns/toggle/bn.ts +70 -0
- package/src/patterns/toggle/en.ts +61 -0
- package/src/patterns/toggle/es.ts +61 -0
- package/src/patterns/toggle/hi.ts +80 -0
- package/src/patterns/toggle/index.ts +95 -0
- package/src/patterns/toggle/it.ts +69 -0
- package/src/patterns/toggle/ja.ts +156 -0
- package/src/patterns/toggle/ko.ts +113 -0
- package/src/patterns/toggle/ms.ts +30 -0
- package/src/patterns/toggle/pl.ts +62 -0
- package/src/patterns/toggle/ru.ts +62 -0
- package/src/patterns/toggle/th.ts +50 -0
- package/src/patterns/toggle/tl.ts +30 -0
- package/src/patterns/toggle/tr.ts +88 -0
- package/src/patterns/toggle/uk.ts +62 -0
- package/src/patterns/toggle/vi.ts +61 -0
- package/src/patterns/toggle/zh.ts +99 -0
- package/src/public-api.ts +286 -0
- package/src/registry.ts +441 -0
- package/src/tokenizers/arabic.ts +723 -0
- package/src/tokenizers/base.ts +1300 -0
- package/src/tokenizers/bengali.ts +289 -0
- package/src/tokenizers/chinese.ts +481 -0
- package/src/tokenizers/english.ts +416 -0
- package/src/tokenizers/french.ts +326 -0
- package/src/tokenizers/german.ts +324 -0
- package/src/tokenizers/hindi.ts +319 -0
- package/src/tokenizers/index.ts +127 -0
- package/src/tokenizers/indonesian.ts +306 -0
- package/src/tokenizers/italian.ts +458 -0
- package/src/tokenizers/japanese.ts +447 -0
- package/src/tokenizers/korean.ts +642 -0
- package/src/tokenizers/morphology/arabic-normalizer.ts +242 -0
- package/src/tokenizers/morphology/french-normalizer.ts +268 -0
- package/src/tokenizers/morphology/german-normalizer.ts +256 -0
- package/src/tokenizers/morphology/index.ts +46 -0
- package/src/tokenizers/morphology/italian-normalizer.ts +329 -0
- package/src/tokenizers/morphology/japanese-normalizer.ts +288 -0
- package/src/tokenizers/morphology/korean-normalizer.ts +428 -0
- package/src/tokenizers/morphology/polish-normalizer.ts +264 -0
- package/src/tokenizers/morphology/portuguese-normalizer.ts +310 -0
- package/src/tokenizers/morphology/spanish-normalizer.ts +327 -0
- package/src/tokenizers/morphology/turkish-normalizer.ts +412 -0
- package/src/tokenizers/morphology/types.ts +211 -0
- package/src/tokenizers/ms.ts +198 -0
- package/src/tokenizers/polish.ts +354 -0
- package/src/tokenizers/portuguese.ts +304 -0
- package/src/tokenizers/quechua.ts +339 -0
- package/src/tokenizers/russian.ts +375 -0
- package/src/tokenizers/spanish.ts +403 -0
- package/src/tokenizers/swahili.ts +303 -0
- package/src/tokenizers/thai.ts +236 -0
- package/src/tokenizers/tl.ts +198 -0
- package/src/tokenizers/turkish.ts +411 -0
- package/src/tokenizers/ukrainian.ts +369 -0
- package/src/tokenizers/vietnamese.ts +410 -0
- package/src/types/grammar-types.ts +617 -0
- package/src/types/unified-profile.ts +267 -0
- package/src/types.ts +709 -0
- package/src/utils/confidence-calculator.ts +147 -0
- package/src/validators/command-validator.ts +380 -0
- package/src/validators/index.ts +15 -0
|
@@ -0,0 +1,573 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Semantic Parser
|
|
3
|
+
*
|
|
4
|
+
* The main parser that converts natural language hyperscript to semantic nodes.
|
|
5
|
+
* Combines tokenization and pattern matching.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import type {
|
|
9
|
+
SemanticNode,
|
|
10
|
+
CommandSemanticNode,
|
|
11
|
+
EventHandlerSemanticNode,
|
|
12
|
+
SemanticParser as ISemanticParser,
|
|
13
|
+
SemanticValue,
|
|
14
|
+
ActionType,
|
|
15
|
+
LanguagePattern,
|
|
16
|
+
LanguageToken,
|
|
17
|
+
} from '../types';
|
|
18
|
+
import { createCommandNode, createEventHandler, createCompoundNode } from '../types';
|
|
19
|
+
import {
|
|
20
|
+
tokenize as tokenizeInternal,
|
|
21
|
+
getSupportedLanguages as getTokenizerLanguages,
|
|
22
|
+
TokenStreamImpl,
|
|
23
|
+
} from '../tokenizers';
|
|
24
|
+
// Import from registry for tree-shaking (registry uses directly-registered patterns first)
|
|
25
|
+
import { getPatternsForLanguage } from '../registry';
|
|
26
|
+
import { patternMatcher } from './pattern-matcher';
|
|
27
|
+
import { render as renderExplicitFn } from '../explicit/renderer';
|
|
28
|
+
import { parseExplicit as parseExplicitFn } from '../explicit/parser';
|
|
29
|
+
|
|
30
|
+
// =============================================================================
|
|
31
|
+
// Semantic Parser Implementation
|
|
32
|
+
// =============================================================================
|
|
33
|
+
|
|
34
|
+
export class SemanticParserImpl implements ISemanticParser {
|
|
35
|
+
/**
|
|
36
|
+
* Parse input in the specified language to a semantic node.
|
|
37
|
+
*/
|
|
38
|
+
parse(input: string, language: string): SemanticNode {
|
|
39
|
+
// Tokenize the input
|
|
40
|
+
const tokens = tokenizeInternal(input, language);
|
|
41
|
+
|
|
42
|
+
// Get patterns for this language
|
|
43
|
+
const patterns = getPatternsForLanguage(language);
|
|
44
|
+
|
|
45
|
+
if (patterns.length === 0) {
|
|
46
|
+
throw new Error(`No patterns available for language: ${language}`);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// Sort patterns by priority (descending)
|
|
50
|
+
const sortedPatterns = [...patterns].sort((a, b) => b.priority - a.priority);
|
|
51
|
+
|
|
52
|
+
// Try to match event handler patterns first (they wrap commands)
|
|
53
|
+
const eventPatterns = sortedPatterns.filter(p => p.command === 'on');
|
|
54
|
+
const eventMatch = patternMatcher.matchBest(tokens, eventPatterns);
|
|
55
|
+
|
|
56
|
+
if (eventMatch) {
|
|
57
|
+
return this.buildEventHandler(eventMatch, tokens, language);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// Try command patterns
|
|
61
|
+
const commandPatterns = sortedPatterns.filter(p => p.command !== 'on');
|
|
62
|
+
const commandMatch = patternMatcher.matchBest(tokens, commandPatterns);
|
|
63
|
+
|
|
64
|
+
if (commandMatch) {
|
|
65
|
+
return this.buildCommand(commandMatch, language);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
throw new Error(`Could not parse input in ${language}: ${input}`);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Check if input can be parsed in the specified language.
|
|
73
|
+
*/
|
|
74
|
+
canParse(input: string, language: string): boolean {
|
|
75
|
+
try {
|
|
76
|
+
this.parse(input, language);
|
|
77
|
+
return true;
|
|
78
|
+
} catch {
|
|
79
|
+
return false;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Get all supported languages.
|
|
85
|
+
*/
|
|
86
|
+
supportedLanguages(): string[] {
|
|
87
|
+
return getTokenizerLanguages();
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Build a command semantic node from a pattern match.
|
|
92
|
+
*/
|
|
93
|
+
private buildCommand(
|
|
94
|
+
match: ReturnType<typeof patternMatcher.matchPattern>,
|
|
95
|
+
language: string
|
|
96
|
+
): CommandSemanticNode {
|
|
97
|
+
if (!match) {
|
|
98
|
+
throw new Error('No match to build command from');
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
const roles: Record<string, SemanticValue> = {};
|
|
102
|
+
for (const [role, value] of match.captured) {
|
|
103
|
+
roles[role] = value;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
return createCommandNode(match.pattern.command, roles, {
|
|
107
|
+
sourceLanguage: language,
|
|
108
|
+
patternId: match.pattern.id,
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Build an event handler semantic node from a pattern match.
|
|
114
|
+
*/
|
|
115
|
+
private buildEventHandler(
|
|
116
|
+
match: ReturnType<typeof patternMatcher.matchPattern>,
|
|
117
|
+
tokens: ReturnType<typeof tokenizeInternal>,
|
|
118
|
+
language: string
|
|
119
|
+
): EventHandlerSemanticNode {
|
|
120
|
+
if (!match) {
|
|
121
|
+
throw new Error('No match to build event handler from');
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// Extract the event name
|
|
125
|
+
const eventValue = match.captured.get('event');
|
|
126
|
+
if (!eventValue) {
|
|
127
|
+
throw new Error('Event handler pattern matched but no event captured');
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// Extract event modifiers (.once, .debounce(), .throttle(), etc.)
|
|
131
|
+
const eventModifiers = patternMatcher.extractEventModifiers(tokens);
|
|
132
|
+
|
|
133
|
+
let body: SemanticNode[];
|
|
134
|
+
|
|
135
|
+
// Check if pattern captured an action (grammar-transformed patterns)
|
|
136
|
+
// These patterns combine event + action in a single match
|
|
137
|
+
const actionValue = match.captured.get('action');
|
|
138
|
+
if (actionValue && actionValue.type === 'literal') {
|
|
139
|
+
// Create a command node directly from captured roles
|
|
140
|
+
const actionName = actionValue.value as string;
|
|
141
|
+
const roles: Record<string, SemanticValue> = {};
|
|
142
|
+
|
|
143
|
+
// Copy relevant roles (excluding event, action, and continues which are structural)
|
|
144
|
+
for (const [role, value] of match.captured) {
|
|
145
|
+
if (role !== 'event' && role !== 'action' && role !== 'continues') {
|
|
146
|
+
roles[role] = value;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
const commandNode = createCommandNode(actionName as ActionType, roles, {
|
|
151
|
+
sourceLanguage: language,
|
|
152
|
+
patternId: match.pattern.id,
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
// Check if pattern has continuation marker (then-chains)
|
|
156
|
+
const continuesValue = match.captured.get('continues');
|
|
157
|
+
if (continuesValue && continuesValue.type === 'literal' && continuesValue.value === 'then') {
|
|
158
|
+
// Parse remaining tokens as additional commands
|
|
159
|
+
const commandPatterns = getPatternsForLanguage(language)
|
|
160
|
+
.filter(p => p.command !== 'on')
|
|
161
|
+
.sort((a, b) => b.priority - a.priority);
|
|
162
|
+
|
|
163
|
+
// Include grammar-transformed continuation patterns (these have specific command types)
|
|
164
|
+
// Continuation patterns have command !== 'on' and id includes 'continuation'
|
|
165
|
+
const grammarContinuationPatterns = getPatternsForLanguage(language)
|
|
166
|
+
.filter(p => p.id.startsWith('grammar-') && p.id.includes('-continuation'))
|
|
167
|
+
.sort((a, b) => b.priority - a.priority);
|
|
168
|
+
|
|
169
|
+
const remainingCommands = this.parseBodyWithGrammarPatterns(
|
|
170
|
+
tokens,
|
|
171
|
+
commandPatterns,
|
|
172
|
+
grammarContinuationPatterns,
|
|
173
|
+
language
|
|
174
|
+
);
|
|
175
|
+
|
|
176
|
+
if (remainingCommands.length > 0) {
|
|
177
|
+
// Combine first command with remaining commands
|
|
178
|
+
body = [commandNode, ...remainingCommands];
|
|
179
|
+
} else {
|
|
180
|
+
body = [commandNode];
|
|
181
|
+
}
|
|
182
|
+
} else {
|
|
183
|
+
body = [commandNode];
|
|
184
|
+
}
|
|
185
|
+
} else {
|
|
186
|
+
// Traditional parsing: parse remaining tokens as body commands
|
|
187
|
+
const commandPatterns = getPatternsForLanguage(language)
|
|
188
|
+
.filter(p => p.command !== 'on')
|
|
189
|
+
.sort((a, b) => b.priority - a.priority);
|
|
190
|
+
|
|
191
|
+
// Use parseBodyWithClauses() to properly handle multi-clause then-chains
|
|
192
|
+
body = this.parseBodyWithClauses(tokens, commandPatterns, language);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
return createEventHandler(eventValue, body, eventModifiers, {
|
|
196
|
+
sourceLanguage: language,
|
|
197
|
+
patternId: match.pattern.id,
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* Parse body with proper clause separation.
|
|
203
|
+
* Splits the token stream at conjunction boundaries (then/それから/ثم/etc.)
|
|
204
|
+
* and parses each clause independently.
|
|
205
|
+
*
|
|
206
|
+
* This handles multi-clause patterns like:
|
|
207
|
+
* - "toggle .active then remove .hidden"
|
|
208
|
+
* - ".active を 切り替え それから .hidden を 削除"
|
|
209
|
+
* - "بدل .active ثم احذف .hidden"
|
|
210
|
+
*
|
|
211
|
+
* @param tokens Token stream to parse
|
|
212
|
+
* @param commandPatterns Command patterns for the language
|
|
213
|
+
* @param language Language code
|
|
214
|
+
* @returns Array of semantic nodes (one per clause)
|
|
215
|
+
*/
|
|
216
|
+
private parseBodyWithClauses(
|
|
217
|
+
tokens: ReturnType<typeof tokenizeInternal>,
|
|
218
|
+
commandPatterns: LanguagePattern[],
|
|
219
|
+
language: string
|
|
220
|
+
): SemanticNode[] {
|
|
221
|
+
const clauses: SemanticNode[] = [];
|
|
222
|
+
const currentClauseTokens: LanguageToken[] = [];
|
|
223
|
+
|
|
224
|
+
while (!tokens.isAtEnd()) {
|
|
225
|
+
const current = tokens.peek();
|
|
226
|
+
if (!current) break;
|
|
227
|
+
|
|
228
|
+
// Check if this is a conjunction token (clause boundary)
|
|
229
|
+
const isConjunction =
|
|
230
|
+
current.kind === 'conjunction' ||
|
|
231
|
+
(current.kind === 'keyword' && this.isThenKeyword(current.value, language));
|
|
232
|
+
|
|
233
|
+
// Check if this is an 'end' keyword (terminates block)
|
|
234
|
+
const isEnd = current.kind === 'keyword' && this.isEndKeyword(current.value, language);
|
|
235
|
+
|
|
236
|
+
if (isConjunction) {
|
|
237
|
+
// We've reached a clause boundary - parse accumulated tokens
|
|
238
|
+
if (currentClauseTokens.length > 0) {
|
|
239
|
+
const clauseNodes = this.parseClause(currentClauseTokens, commandPatterns, language);
|
|
240
|
+
clauses.push(...clauseNodes);
|
|
241
|
+
currentClauseTokens.length = 0; // Clear for next clause
|
|
242
|
+
}
|
|
243
|
+
tokens.advance(); // Consume conjunction token
|
|
244
|
+
continue;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
if (isEnd) {
|
|
248
|
+
// End of block - parse final clause if any
|
|
249
|
+
if (currentClauseTokens.length > 0) {
|
|
250
|
+
const clauseNodes = this.parseClause(currentClauseTokens, commandPatterns, language);
|
|
251
|
+
clauses.push(...clauseNodes);
|
|
252
|
+
}
|
|
253
|
+
tokens.advance(); // Consume 'end' token
|
|
254
|
+
break;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
// Accumulate token for current clause
|
|
258
|
+
currentClauseTokens.push(current);
|
|
259
|
+
tokens.advance();
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
// Parse any remaining tokens as final clause
|
|
263
|
+
if (currentClauseTokens.length > 0) {
|
|
264
|
+
const clauseNodes = this.parseClause(currentClauseTokens, commandPatterns, language);
|
|
265
|
+
clauses.push(...clauseNodes);
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
// If we have multiple clauses, wrap in CompoundSemanticNode
|
|
269
|
+
if (clauses.length > 1) {
|
|
270
|
+
return [createCompoundNode(clauses, 'then', { sourceLanguage: language })];
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
return clauses;
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
/**
|
|
277
|
+
* Parse a single clause (sequence of tokens between conjunctions).
|
|
278
|
+
* Returns array of semantic nodes parsed from the clause.
|
|
279
|
+
*/
|
|
280
|
+
private parseClause(
|
|
281
|
+
clauseTokens: LanguageToken[],
|
|
282
|
+
commandPatterns: LanguagePattern[],
|
|
283
|
+
language: string
|
|
284
|
+
): SemanticNode[] {
|
|
285
|
+
if (clauseTokens.length === 0) {
|
|
286
|
+
return [];
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
// Create a TokenStream from the clause tokens
|
|
290
|
+
const clauseStream = new TokenStreamImpl(clauseTokens, language);
|
|
291
|
+
const commands: SemanticNode[] = [];
|
|
292
|
+
|
|
293
|
+
while (!clauseStream.isAtEnd()) {
|
|
294
|
+
// Try to match as a command
|
|
295
|
+
const commandMatch = patternMatcher.matchBest(clauseStream, commandPatterns);
|
|
296
|
+
if (commandMatch) {
|
|
297
|
+
commands.push(this.buildCommand(commandMatch, language));
|
|
298
|
+
} else {
|
|
299
|
+
// Skip unrecognized token
|
|
300
|
+
clauseStream.advance();
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
return commands;
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
/**
|
|
308
|
+
* Parse body commands with support for grammar-transformed patterns.
|
|
309
|
+
* Used after a grammar-transformed pattern with continuation marker.
|
|
310
|
+
*/
|
|
311
|
+
private parseBodyWithGrammarPatterns(
|
|
312
|
+
tokens: ReturnType<typeof tokenizeInternal>,
|
|
313
|
+
commandPatterns: LanguagePattern[],
|
|
314
|
+
grammarPatterns: LanguagePattern[],
|
|
315
|
+
language: string
|
|
316
|
+
): SemanticNode[] {
|
|
317
|
+
const commands: SemanticNode[] = [];
|
|
318
|
+
|
|
319
|
+
while (!tokens.isAtEnd()) {
|
|
320
|
+
const current = tokens.peek();
|
|
321
|
+
|
|
322
|
+
// Check for 'then' keyword - skip it and continue parsing
|
|
323
|
+
if (current && this.isThenKeyword(current.value, language)) {
|
|
324
|
+
tokens.advance();
|
|
325
|
+
continue;
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
// Check for 'end' keyword - terminates block
|
|
329
|
+
if (current && this.isEndKeyword(current.value, language)) {
|
|
330
|
+
tokens.advance();
|
|
331
|
+
break;
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
let matched = false;
|
|
335
|
+
|
|
336
|
+
// Try grammar-transformed continuation patterns first
|
|
337
|
+
// These patterns have command set to the actual command type (e.g., 'remove', 'toggle')
|
|
338
|
+
if (grammarPatterns.length > 0) {
|
|
339
|
+
const grammarMatch = patternMatcher.matchBest(tokens, grammarPatterns);
|
|
340
|
+
if (grammarMatch) {
|
|
341
|
+
// Use the pattern's command field as the action
|
|
342
|
+
const actionName = grammarMatch.pattern.command;
|
|
343
|
+
const roles: Record<string, SemanticValue> = {};
|
|
344
|
+
|
|
345
|
+
// Copy relevant roles (excluding structural roles)
|
|
346
|
+
for (const [role, value] of grammarMatch.captured) {
|
|
347
|
+
if (role !== 'event' && role !== 'action' && role !== 'continues') {
|
|
348
|
+
roles[role] = value;
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
const commandNode = createCommandNode(actionName as ActionType, roles, {
|
|
353
|
+
sourceLanguage: language,
|
|
354
|
+
patternId: grammarMatch.pattern.id,
|
|
355
|
+
});
|
|
356
|
+
commands.push(commandNode);
|
|
357
|
+
matched = true;
|
|
358
|
+
|
|
359
|
+
// Check if this pattern also has continuation
|
|
360
|
+
const continuesValue = grammarMatch.captured.get('continues');
|
|
361
|
+
if (
|
|
362
|
+
continuesValue &&
|
|
363
|
+
continuesValue.type === 'literal' &&
|
|
364
|
+
continuesValue.value === 'then'
|
|
365
|
+
) {
|
|
366
|
+
// Continue parsing for more commands
|
|
367
|
+
continue;
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
// Try regular command patterns
|
|
373
|
+
if (!matched) {
|
|
374
|
+
const commandMatch = patternMatcher.matchBest(tokens, commandPatterns);
|
|
375
|
+
if (commandMatch) {
|
|
376
|
+
commands.push(this.buildCommand(commandMatch, language));
|
|
377
|
+
matched = true;
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
// Skip unrecognized token
|
|
382
|
+
if (!matched) {
|
|
383
|
+
tokens.advance();
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
return commands;
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
/**
|
|
391
|
+
* Check if a token is a 'then' keyword in the given language.
|
|
392
|
+
*/
|
|
393
|
+
private isThenKeyword(value: string, language: string): boolean {
|
|
394
|
+
const thenKeywords: Record<string, Set<string>> = {
|
|
395
|
+
en: new Set(['then']),
|
|
396
|
+
ja: new Set(['それから', '次に', 'そして']),
|
|
397
|
+
ar: new Set(['ثم', 'بعدها', 'ثمّ']),
|
|
398
|
+
es: new Set(['entonces', 'luego', 'después']),
|
|
399
|
+
ko: new Set(['그다음', '그리고', '그런후']),
|
|
400
|
+
zh: new Set(['然后', '接着', '之后']),
|
|
401
|
+
tr: new Set(['sonra', 'ardından', 'daha sonra']),
|
|
402
|
+
pt: new Set(['então', 'depois', 'logo']),
|
|
403
|
+
fr: new Set(['puis', 'ensuite', 'alors']),
|
|
404
|
+
de: new Set(['dann', 'danach', 'anschließend']),
|
|
405
|
+
id: new Set(['lalu', 'kemudian', 'setelah itu']),
|
|
406
|
+
qu: new Set(['chaymantataq', 'hinaspa', 'chaymanta']),
|
|
407
|
+
sw: new Set(['kisha', 'halafu', 'baadaye']),
|
|
408
|
+
};
|
|
409
|
+
const keywords = thenKeywords[language] || thenKeywords.en;
|
|
410
|
+
return keywords.has(value.toLowerCase());
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
/**
|
|
414
|
+
* Check if a token is an 'end' keyword in the given language.
|
|
415
|
+
*/
|
|
416
|
+
private isEndKeyword(value: string, language: string): boolean {
|
|
417
|
+
const endKeywords: Record<string, Set<string>> = {
|
|
418
|
+
en: new Set(['end']),
|
|
419
|
+
ja: new Set(['終わり', '終了', 'おわり']),
|
|
420
|
+
ar: new Set(['نهاية', 'انتهى', 'آخر']),
|
|
421
|
+
es: new Set(['fin', 'final', 'terminar']),
|
|
422
|
+
ko: new Set(['끝', '종료', '마침']),
|
|
423
|
+
zh: new Set(['结束', '终止', '完']),
|
|
424
|
+
tr: new Set(['son', 'bitiş', 'bitti']),
|
|
425
|
+
pt: new Set(['fim', 'final', 'término']),
|
|
426
|
+
fr: new Set(['fin', 'terminer', 'finir']),
|
|
427
|
+
de: new Set(['ende', 'beenden', 'fertig']),
|
|
428
|
+
id: new Set(['selesai', 'akhir', 'tamat']),
|
|
429
|
+
qu: new Set(['tukukuy', 'tukuy', 'puchukay']),
|
|
430
|
+
sw: new Set(['mwisho', 'maliza', 'tamati']),
|
|
431
|
+
};
|
|
432
|
+
const keywords = endKeywords[language] || endKeywords.en;
|
|
433
|
+
return keywords.has(value.toLowerCase());
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
// =============================================================================
|
|
438
|
+
// Convenience Functions
|
|
439
|
+
// =============================================================================
|
|
440
|
+
|
|
441
|
+
/**
|
|
442
|
+
* Singleton parser instance.
|
|
443
|
+
*/
|
|
444
|
+
export const semanticParser = new SemanticParserImpl();
|
|
445
|
+
|
|
446
|
+
/**
|
|
447
|
+
* Parse input in the specified language.
|
|
448
|
+
*/
|
|
449
|
+
export function parse(input: string, language: string): SemanticNode {
|
|
450
|
+
return semanticParser.parse(input, language);
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
/**
|
|
454
|
+
* Check if input can be parsed.
|
|
455
|
+
*/
|
|
456
|
+
export function canParse(input: string, language: string): boolean {
|
|
457
|
+
return semanticParser.canParse(input, language);
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
/**
|
|
461
|
+
* Parse and return command type if parseable.
|
|
462
|
+
*/
|
|
463
|
+
export function getCommandType(input: string, language: string): ActionType | null {
|
|
464
|
+
try {
|
|
465
|
+
const node = semanticParser.parse(input, language);
|
|
466
|
+
return node.action;
|
|
467
|
+
} catch {
|
|
468
|
+
return null;
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
// =============================================================================
|
|
473
|
+
// Additional Public API Functions
|
|
474
|
+
// =============================================================================
|
|
475
|
+
|
|
476
|
+
/**
|
|
477
|
+
* Tokenize input for a specific language.
|
|
478
|
+
*/
|
|
479
|
+
export function tokenize(input: string, language: string) {
|
|
480
|
+
return tokenizeInternal(input, language);
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
/**
|
|
484
|
+
* Get list of supported languages.
|
|
485
|
+
*/
|
|
486
|
+
export function getSupportedLanguages(): string[] {
|
|
487
|
+
return getTokenizerLanguages();
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
/**
|
|
491
|
+
* Translate hyperscript between languages.
|
|
492
|
+
*/
|
|
493
|
+
export function translate(input: string, sourceLang: string, targetLang: string): string {
|
|
494
|
+
const node = parse(input, sourceLang);
|
|
495
|
+
return render(node, targetLang);
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
/**
|
|
499
|
+
* Get translations for all supported languages.
|
|
500
|
+
*/
|
|
501
|
+
export function getAllTranslations(input: string, sourceLang: string): Record<string, string> {
|
|
502
|
+
const node = parse(input, sourceLang);
|
|
503
|
+
const result: Record<string, string> = {};
|
|
504
|
+
for (const lang of getSupportedLanguages()) {
|
|
505
|
+
try {
|
|
506
|
+
result[lang] = render(node, lang);
|
|
507
|
+
} catch {
|
|
508
|
+
// Skip languages that can't render this command
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
return result;
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
/**
|
|
515
|
+
* Create a semantic analyzer for parsing with confidence scores.
|
|
516
|
+
*/
|
|
517
|
+
export function createSemanticAnalyzer() {
|
|
518
|
+
return {
|
|
519
|
+
analyze(input: string, language: string) {
|
|
520
|
+
try {
|
|
521
|
+
const node = parse(input, language);
|
|
522
|
+
return { node, confidence: 1.0, success: true };
|
|
523
|
+
} catch (error) {
|
|
524
|
+
return { node: null, confidence: 0, success: false, error };
|
|
525
|
+
}
|
|
526
|
+
},
|
|
527
|
+
};
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
/**
|
|
531
|
+
* Render a SemanticNode to hyperscript in a specific language.
|
|
532
|
+
*/
|
|
533
|
+
export function render(node: SemanticNode, language: string): string {
|
|
534
|
+
return renderExplicitFn(node, language);
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
/**
|
|
538
|
+
* Render a SemanticNode in explicit syntax format.
|
|
539
|
+
*/
|
|
540
|
+
export function renderExplicit(node: SemanticNode): string {
|
|
541
|
+
return renderExplicitFn(node, 'explicit');
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
/**
|
|
545
|
+
* Parse explicit syntax format.
|
|
546
|
+
*/
|
|
547
|
+
export function parseExplicit(input: string): SemanticNode {
|
|
548
|
+
return parseExplicitFn(input);
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
/**
|
|
552
|
+
* Convert natural language to explicit syntax.
|
|
553
|
+
*/
|
|
554
|
+
export function toExplicit(input: string, language: string): string {
|
|
555
|
+
const node = parse(input, language);
|
|
556
|
+
return renderExplicit(node);
|
|
557
|
+
}
|
|
558
|
+
|
|
559
|
+
/**
|
|
560
|
+
* Convert explicit syntax to natural language.
|
|
561
|
+
*/
|
|
562
|
+
export function fromExplicit(input: string, targetLang: string): string {
|
|
563
|
+
const node = parseExplicit(input);
|
|
564
|
+
return render(node, targetLang);
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
/**
|
|
568
|
+
* Round-trip conversion for testing.
|
|
569
|
+
*/
|
|
570
|
+
export function roundTrip(input: string, language: string): string {
|
|
571
|
+
const explicit = toExplicit(input, language);
|
|
572
|
+
return fromExplicit(explicit, language);
|
|
573
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Parser Utilities
|
|
3
|
+
*
|
|
4
|
+
* Shared utilities for pattern-generator and pattern-matcher.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
export {
|
|
8
|
+
ROLE_PRIORITY,
|
|
9
|
+
getRolePriority,
|
|
10
|
+
sortRolesByPriority,
|
|
11
|
+
sortRolesByWordOrder,
|
|
12
|
+
type WordOrder,
|
|
13
|
+
type RoleWithPosition,
|
|
14
|
+
} from './role-positioning';
|
|
15
|
+
|
|
16
|
+
export {
|
|
17
|
+
resolveMarkerForRole,
|
|
18
|
+
getAllMarkersForRole,
|
|
19
|
+
getDefaultRoleMarker,
|
|
20
|
+
type RoleSpecWithMarker,
|
|
21
|
+
type ResolvedMarker,
|
|
22
|
+
} from './marker-resolution';
|
|
23
|
+
|
|
24
|
+
export {
|
|
25
|
+
isTypeCompatible,
|
|
26
|
+
validateValueType,
|
|
27
|
+
isCSSSelector,
|
|
28
|
+
isClassName,
|
|
29
|
+
isIdSelector,
|
|
30
|
+
isCSSPropertyRef,
|
|
31
|
+
isNumericValue,
|
|
32
|
+
isPropertyName,
|
|
33
|
+
isVariableRef,
|
|
34
|
+
isBuiltInReference,
|
|
35
|
+
} from './type-validation';
|