unexpectedly 1.0.0 → 3.0.3

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/testFile.peggy ADDED
@@ -0,0 +1,111 @@
1
+ {{
2
+ import { mapObj } from './utils.js'
3
+ }}
4
+
5
+ {
6
+ let vars = {};
7
+ }
8
+ start
9
+ = all:(WS @line? _)|.., EOL| {
10
+ return all.filter(t => t && t.hasOwnProperty('expected'));
11
+ }
12
+
13
+ line
14
+ = var
15
+ / comment
16
+ / test
17
+ / '' // hack
18
+
19
+ var
20
+ = '#!' env:"!"? _ name:chunk _ ':' _ value:(@val _)? {
21
+ if (!value) {
22
+ const start = location().start;
23
+ value = {
24
+ value: undefined,
25
+ line: start.line,
26
+ column: start.column,
27
+ }
28
+ }
29
+ value.env = Boolean(env);
30
+ vars = {
31
+ ...vars,
32
+ [name]: value
33
+ }
34
+ }
35
+
36
+ val
37
+ = value:chunk {
38
+ const start = location().start;
39
+ return { value, line: start.line, column: start.column}
40
+ }
41
+
42
+ test
43
+ = expected:results inputs:(LWS @chunk)* comment? {
44
+ const start = location().start;
45
+ return { expected, inputs, vars, line: start.line, column: start.column }
46
+ }
47
+
48
+ results
49
+ = "!" { return options.EXCEPTION || "!" }
50
+ / chunk
51
+
52
+ chunk
53
+ = quoted
54
+ / hex
55
+ / word
56
+
57
+ quoted
58
+ = '"' info:dqchar* '"' { return info.join('') }
59
+ / "'" info:sqchar* "'" { return info.join('') }
60
+ / "`" info:bqchar* "`" { return info.join('') }
61
+
62
+ dqchar "characters that go inside double quotes, including escaped dquotes"
63
+ = '\\"' { return '"' }
64
+ / qchar
65
+ / $[^\\"]+
66
+
67
+ sqchar "characters that go inside single quotes, including escaped squotes"
68
+ = "\\'" { return "'" }
69
+ / qchar
70
+ / $[^\\']+
71
+
72
+ bqchar "characters that go inside back quotes, including escaped bquotes"
73
+ = "\\`" { return "`" }
74
+ / qchar
75
+ / $[^\\`]+
76
+
77
+ hex
78
+ = '0x' hx:$[a-fA-F0-9]+ { return '0x' + hx.toLowerCase(hx) }
79
+
80
+ word
81
+ = chars:wchar+ { return chars.join('') }
82
+
83
+ wchar "words can contain quoted bits"
84
+ = qchar+
85
+ / $[^ #:'"\t\r\n]+
86
+
87
+ qchar "other quoted characters, as needed"
88
+ = '\\\\' { return '\\' }
89
+ / '\\r' { return '\r' }
90
+ / '\\n' { return '\n' }
91
+ / '\\t' { return '\t' }
92
+ / '\\#' { return '#' }
93
+ / '\\:' { return ':' }
94
+ / '\\ ' { return ' ' }
95
+
96
+ comment
97
+ = WS "#" !"!" [^\r\n]* { return undefined }
98
+
99
+ _ "optional whitespace"
100
+ = [ \t]* { return undefined }
101
+
102
+ WS "optional whitespace, including newlines"
103
+ = [ \t\r\n]* { return undefined}
104
+
105
+ LWS "required whitespace"
106
+ = [ \t]+ { return undefined }
107
+
108
+ EOL "End of line"
109
+ = '\n'
110
+ / '\r\n'
111
+ / '\r'
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2010-2024 The Peggy AUTHORS
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,81 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * When used as a grammarSource, allows grammars embedded in larger files to
5
+ * specify their offset. The start location is the first character in the
6
+ * grammar. The first line is often moved to the right by some number of
7
+ * columns, but subsequent lines all start at the first column.
8
+ */
9
+ class GrammarLocation {
10
+ /**
11
+ * Create an instance.
12
+ *
13
+ * @param {any} source The original grammarSource. Should be a string or
14
+ * have a toString() method.
15
+ * @param {import("./peg").Location} start The starting offset for the
16
+ * grammar in the larger file.
17
+ */
18
+ constructor(source, start) {
19
+ this.source = source;
20
+ this.start = start;
21
+ }
22
+
23
+ /**
24
+ * Coerce to a string.
25
+ *
26
+ * @returns {string} The source, stringified.
27
+ */
28
+ toString() {
29
+ return String(this.source);
30
+ }
31
+
32
+ /**
33
+ * Return a new Location offset from the given location by the start of the
34
+ * grammar.
35
+ *
36
+ * @param {import("./peg").Location} loc The location as if the start of the
37
+ * grammar was the start of the file.
38
+ * @returns {import("./peg").Location} The offset location.
39
+ */
40
+ offset(loc) {
41
+ return {
42
+ line: loc.line + this.start.line - 1,
43
+ column: (loc.line === 1) ?
44
+ loc.column + this.start.column - 1 :
45
+ loc.column,
46
+ offset: loc.offset + this.start.offset,
47
+ };
48
+ }
49
+
50
+ /**
51
+ * If the range has a grammarSource that is a GrammarLocation, offset the
52
+ * start of that range by the GrammarLocation.
53
+ *
54
+ * @param {import("./peg").LocationRange} range The range to extract from.
55
+ * @returns {import("./peg").Location} The offset start if possible, or the
56
+ * original start.
57
+ */
58
+ static offsetStart(range) {
59
+ if (range.source && (typeof range.source.offset === 'function')) {
60
+ return range.source.offset(range.start);
61
+ }
62
+ return range.start;
63
+ }
64
+
65
+ /**
66
+ * If the range has a grammarSource that is a GrammarLocation, offset the
67
+ * end of that range by the GrammarLocation.
68
+ *
69
+ * @param {import("./peg").LocationRange} range The range to extract from.
70
+ * @returns {import("./peg").Location} The offset end if possible, or the
71
+ * original end.
72
+ */
73
+ static offsetEnd(range) {
74
+ if (range.source && (typeof range.source.offset === 'function')) {
75
+ return range.source.offset(range.end);
76
+ }
77
+ return range.end;
78
+ }
79
+ }
80
+
81
+ module.exports = GrammarLocation;
@@ -0,0 +1,4 @@
1
+ I don't want to take a runtime dependency on Peggy just for the
2
+ GrammarLocation class. If this works, I may refactor that into
3
+ @peggyjs/grammar-location, but I'm worried about making that change completely
4
+ transparent to Peggy users.
package/version.js ADDED
@@ -0,0 +1,4 @@
1
+ // Generated by package-extract v3.1.3
2
+ // Do not modify by hand.
3
+
4
+ export const version = '3.0.3';
package/modtype.js DELETED
@@ -1,60 +0,0 @@
1
- import fs from 'node:fs/promises';
2
- import path from 'node:path';
3
-
4
- export class ModType {
5
- static #cache = new Map(); // Filename => 'commonjs', 'module', 'unknown'
6
-
7
- static async #readPkg(dir) {
8
- const pkg = path.join(dir, 'package.json');
9
- try {
10
- const {type = 'commonjs'} = JSON.parse(await fs.readFile(pkg, 'utf8'));
11
- return type;
12
- } catch {
13
- return undefined;
14
- }
15
- }
16
-
17
- static async find(name, dir = false, root = undefined) {
18
- const prev = this.#cache.get(name);
19
- if (prev) {
20
- return prev;
21
- }
22
- let res = 'unknown';
23
-
24
- if (!root) {
25
- const p = path.parse(name);
26
- switch (p.ext) {
27
- case '.mjs':
28
- res = 'module';
29
- this.#cache.set(name, res);
30
- return res;
31
- case '.cjs':
32
- res = 'commonjs';
33
- this.#cache.set(name, res);
34
- return res;
35
- }
36
- ({root} = p);
37
- try {
38
- // Avoid problems if an existing directory name is passed in originally.
39
- dir = (await fs.stat(name)).isDirectory();
40
- } catch {
41
- // File doesn't exist. Safe to assume it's file-ish, keep dir = false.
42
- }
43
- }
44
-
45
- if (name !== root) { // Never go all the way to the root directory.
46
- const t = dir ? await this.#readPkg(name) : undefined;
47
- res = t ?? this.find(path.dirname(name), true, root);
48
- }
49
- this.#cache.set(name, res);
50
- return res;
51
- }
52
-
53
- static clear() {
54
- this.#cache.clear();
55
- }
56
-
57
- static get cacheSize() {
58
- return this.#cache.size;
59
- }
60
- }