@repometry/language-python 0.0.0-dev.0 → 0.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/package.json +32 -2
- package/target/main/resources/grammar.json +7 -0
- package/target/main/resources/tree-sitter.LICENSE +21 -0
- package/target/main/resources/tree-sitter.wasm +0 -0
- package/target/main/ts/index.d.ts +1 -0
- package/target/main/ts/index.js +2 -0
- package/target/main/ts/python-source.d.ts +2 -0
- package/target/main/ts/python-source.js +36 -0
- package/target/main/ts/python-syntax.d.ts +2 -0
- package/target/main/ts/python-syntax.js +8 -0
- package/target/main/ts/python.d.ts +2 -0
- package/target/main/ts/python.js +90 -0
- package/README.md +0 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Idleware
|
|
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.
|
package/package.json
CHANGED
|
@@ -1,14 +1,44 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@repometry/language-python",
|
|
3
|
-
"version": "0.0.0
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "0.0.0",
|
|
4
|
+
"description": "Python for the Repometry tree-sitter plugin, with its grammar",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
7
7
|
"type": "git",
|
|
8
8
|
"url": "git+https://github.com/idleware/repometry.git"
|
|
9
9
|
},
|
|
10
10
|
"homepage": "https://github.com/idleware/repometry#readme",
|
|
11
|
+
"bugs": {
|
|
12
|
+
"url": "https://github.com/idleware/repometry/issues"
|
|
13
|
+
},
|
|
14
|
+
"keywords": [
|
|
15
|
+
"repometry",
|
|
16
|
+
"tree-sitter",
|
|
17
|
+
"python"
|
|
18
|
+
],
|
|
19
|
+
"type": "module",
|
|
20
|
+
"engines": {
|
|
21
|
+
"node": ">=24"
|
|
22
|
+
},
|
|
23
|
+
"exports": {
|
|
24
|
+
".": {
|
|
25
|
+
"types": "./target/main/ts/index.d.ts",
|
|
26
|
+
"default": "./target/main/ts/index.js"
|
|
27
|
+
}
|
|
28
|
+
},
|
|
29
|
+
"files": [
|
|
30
|
+
"target/main",
|
|
31
|
+
"!target/main/**/*.map"
|
|
32
|
+
],
|
|
11
33
|
"publishConfig": {
|
|
12
34
|
"access": "public"
|
|
35
|
+
},
|
|
36
|
+
"scripts": {
|
|
37
|
+
"build": "node -e \"require('node:fs').rmSync('target/main', { recursive: true, force: true })\" && tsc -p tsconfig.json && node -e \"const fs = require('node:fs'); fs.cpSync('src/main/resources', 'target/main/resources', { recursive: true }); fs.cpSync('../../LICENSE', 'LICENSE')\"",
|
|
38
|
+
"prepack": "npm run build"
|
|
39
|
+
},
|
|
40
|
+
"dependencies": {
|
|
41
|
+
"@repometry/core": "0.0.0",
|
|
42
|
+
"@repometry/tree-sitter": "0.0.0"
|
|
13
43
|
}
|
|
14
44
|
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"package": "tree-sitter-python",
|
|
3
|
+
"version": "0.25.0",
|
|
4
|
+
"license": "MIT",
|
|
5
|
+
"integrity": "sha512-eCmJx6zQa35GxaCtQD+wXHOhYqBxEL+bp71W/s3fcDMu06MrtzkVXR437dRrCrbrDbyLuUDJpAgycs7ncngLXw==",
|
|
6
|
+
"sha256": "16108b50df4ee9a30168794252ab55e7c93bfc5765d7fa0aa3e335752c515f47"
|
|
7
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2016 Max Brunsfeld
|
|
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.
|
|
Binary file
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { python } from './python.js';
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
const aliases = { 'iso-latin-1': 'latin-1', 'latin-1-unix': 'latin-1',
|
|
2
|
+
'utf-8-mac': 'utf-8', 'mac-roman': 'macintosh', big5hkscs: 'big5-hkscs' };
|
|
3
|
+
const cookie = /^[ \t\f]*#.*?coding[:=][ \t]*([-_.A-Za-z\d]+)/;
|
|
4
|
+
/** PEP 263 reads the first two physical lines as ASCII before choosing the Python source encoding. */
|
|
5
|
+
export function pythonSource(bytes) {
|
|
6
|
+
const buffer = Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
7
|
+
const bom = buffer[0] === 0xef && buffer[1] === 0xbb && buffer[2] === 0xbf;
|
|
8
|
+
const from = bom ? 3 : 0;
|
|
9
|
+
const firstEnd = lineEnd(buffer, from);
|
|
10
|
+
const first = buffer.toString('latin1', from, firstEnd);
|
|
11
|
+
const secondStart = firstEnd + (buffer[firstEnd] === 13 && buffer[firstEnd + 1] === 10 ? 2 : 1);
|
|
12
|
+
const second = /^[ \t\f]*(?:#|$)/.test(first) ? buffer.toString('latin1', secondStart, lineEnd(buffer, secondStart)) : '';
|
|
13
|
+
const label = (cookie.exec(first)?.[1] ?? cookie.exec(second)?.[1] ?? 'utf-8').toLowerCase().replaceAll('_', '-');
|
|
14
|
+
const encoding = aliases[label] ?? label;
|
|
15
|
+
if (bom && !['utf-8', 'utf8', 'utf-8-sig'].includes(encoding))
|
|
16
|
+
throw new SyntaxError('UTF-8 signature disagrees with the encoding declaration');
|
|
17
|
+
return decode(buffer, encoding);
|
|
18
|
+
}
|
|
19
|
+
function lineEnd(buffer, start) {
|
|
20
|
+
for (let offset = start; offset < buffer.length; offset++) {
|
|
21
|
+
if (buffer[offset] === 10 || buffer[offset] === 13)
|
|
22
|
+
return offset;
|
|
23
|
+
}
|
|
24
|
+
return buffer.length;
|
|
25
|
+
}
|
|
26
|
+
function decode(buffer, encoding) {
|
|
27
|
+
if (/^(?:latin-?1|iso-?8859-?1|latin)$/.test(encoding))
|
|
28
|
+
return buffer.toString('latin1');
|
|
29
|
+
if (encoding === 'ascii' || encoding === 'us-ascii') {
|
|
30
|
+
if (buffer.some(byte => byte > 127))
|
|
31
|
+
throw new SyntaxError('Non-ASCII bytes in an ASCII source file');
|
|
32
|
+
return buffer.toString('ascii');
|
|
33
|
+
}
|
|
34
|
+
return new TextDecoder(encoding === 'utf-8-sig' ? 'utf-8' : encoding, { fatal: true }).decode(buffer);
|
|
35
|
+
}
|
|
36
|
+
//# sourceMappingURL=python-source.js.map
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export const pythonSyntax = { lineComments: ['#'],
|
|
2
|
+
strings: [{ quote: '"""', multiline: true }, { quote: "'''", multiline: true }, { quote: '"' }, { quote: "'" }],
|
|
3
|
+
stringPrefixes: ['r', 'u', 'b', 'f', 't', 'br', 'rb', 'fr', 'rf', 'tr', 'rt'],
|
|
4
|
+
keywords: ['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break', 'class', 'continue', 'def', 'del',
|
|
5
|
+
'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not',
|
|
6
|
+
'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield'],
|
|
7
|
+
};
|
|
8
|
+
//# sourceMappingURL=python-syntax.js.map
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { measures } from '@repometry/tree-sitter';
|
|
2
|
+
import manifest from '../resources/grammar.json' with { type: 'json' };
|
|
3
|
+
import { pythonSource } from './python-source.js';
|
|
4
|
+
import { pythonSyntax } from './python-syntax.js';
|
|
5
|
+
function irrefutable(pattern) {
|
|
6
|
+
const pending = [pattern];
|
|
7
|
+
while (pending.length) {
|
|
8
|
+
const node = pending.pop();
|
|
9
|
+
const inner = node.namedChildren;
|
|
10
|
+
if (capturesAll(node, inner))
|
|
11
|
+
return true;
|
|
12
|
+
if (node.type === 'union_pattern') {
|
|
13
|
+
for (const child of inner)
|
|
14
|
+
pending.push(child);
|
|
15
|
+
}
|
|
16
|
+
else {
|
|
17
|
+
const child = groupedPattern(node, inner);
|
|
18
|
+
if (child)
|
|
19
|
+
pending.push(child);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
return false;
|
|
23
|
+
}
|
|
24
|
+
function capturesAll(node, inner) {
|
|
25
|
+
switch (node.type) {
|
|
26
|
+
case 'case_pattern': return inner.length === 0 && node.text === '_';
|
|
27
|
+
case 'dotted_name': return inner.length === 1;
|
|
28
|
+
case 'union_pattern': return node.children.some(child => child.type === '_');
|
|
29
|
+
default: return false;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
function groupedPattern(node, inner) {
|
|
33
|
+
if (node.type === 'as_pattern' || node.type === 'case_pattern')
|
|
34
|
+
return inner[0];
|
|
35
|
+
if (node.type === 'tuple_pattern' && inner.length === 1 && !node.children.some(child => child.type === ','))
|
|
36
|
+
return inner[0];
|
|
37
|
+
}
|
|
38
|
+
function matchDecisions(node) {
|
|
39
|
+
const patterns = node.namedChildren.filter(child => child.type === 'case_pattern');
|
|
40
|
+
const captured = patterns.length === 1 && !node.children.some(child => child.type === ',') && irrefutable(patterns[0]);
|
|
41
|
+
return Number(!captured) + Number(node.childForFieldName('guard') !== null);
|
|
42
|
+
}
|
|
43
|
+
const names = new Set(['match', 'case', 'type', '__future__', 'print', 'exec']);
|
|
44
|
+
// Start with tokenize spellings and omit encoding, comments, indentation, line endings and continuations.
|
|
45
|
+
// Preserve keywords and punctuation. Treat match, case, type, __future__, print and exec as identifiers in
|
|
46
|
+
// all contexts. Retain _ literally, including when it is a wildcard. True and False share a boolean kind.
|
|
47
|
+
// The similar kinds are identifier, boolean, none, string, integer, float and imaginary. Each quoted string
|
|
48
|
+
// is one token; coalesce f-string components when the lexer splits them. A number ending in j/J is imaginary.
|
|
49
|
+
// Files under 12 code lines contribute no clone tokens. Each token is its UTF-16 code-unit length, a colon
|
|
50
|
+
// and the spelling. Similar fingerprints use !kind: for the listed kinds.
|
|
51
|
+
const tokens = {
|
|
52
|
+
candidates: new Set(['assert_statement', 'block', 'break_statement', 'case_clause', 'class_definition', 'continue_statement',
|
|
53
|
+
'delete_statement', 'elif_clause', 'exec_statement', 'except_clause', 'except_group_clause', 'expression_statement', 'for_statement',
|
|
54
|
+
'function_definition', 'global_statement', 'if_statement', 'lambda', 'match_statement', 'nonlocal_statement', 'pass_statement', 'print_statement',
|
|
55
|
+
'raise_statement', 'return_statement', 'try_statement', 'type_alias_statement', 'while_statement', 'with_statement']),
|
|
56
|
+
kinds: new Map([['identifier', 'identifier'], ['string', 'string'],
|
|
57
|
+
['integer', 'integer'], ['float', 'float'], ['true', 'boolean'], ['false', 'boolean'], ['none', 'none']]),
|
|
58
|
+
normalize: (_type, text, kind) => [text, tokenKind(text, kind)],
|
|
59
|
+
};
|
|
60
|
+
function tokenKind(text, kind) {
|
|
61
|
+
if (text === '_')
|
|
62
|
+
return undefined;
|
|
63
|
+
if (names.has(text))
|
|
64
|
+
return 'identifier';
|
|
65
|
+
return (kind === 'integer' || kind === 'float') && /j$/i.test(text) ? 'imaginary' : kind;
|
|
66
|
+
}
|
|
67
|
+
export const python = {
|
|
68
|
+
language: { id: 'python', label: 'Python', color: '#3572a5', extensions: ['.py'], measures: [...measures, 'classes'], syntax: pythonSyntax },
|
|
69
|
+
tool: { package: manifest.package, version: manifest.version },
|
|
70
|
+
asset: new URL('../resources/tree-sitter.wasm', import.meta.url),
|
|
71
|
+
decode: pythonSource, prepareSource: text => text.replace(/\r(?!\n)/g, '\n'),
|
|
72
|
+
trimObservationComments: true, requireNonemptyBlock: 'block',
|
|
73
|
+
columns: 'codepoints', name: node => node.childForFieldName('name')?.text.normalize('NFKC'),
|
|
74
|
+
rules: {
|
|
75
|
+
function_definition: { flow: 'function' }, lambda: { flow: 'function' }, class_definition: { flow: 'class' },
|
|
76
|
+
decorated_definition: { flow: 'decorated' },
|
|
77
|
+
if_statement: { flow: 'if', decision: true }, elif_clause: { flow: 'elif', decision: true }, else_clause: { flow: 'else' },
|
|
78
|
+
for_statement: { flow: 'nest', decision: true }, while_statement: { flow: 'nest', decision: true },
|
|
79
|
+
except_clause: { flow: 'nest', decision: true }, except_group_clause: { flow: 'nest', decision: true },
|
|
80
|
+
conditional_expression: { flow: 'nest', decision: true, order: [1, 0, 2] },
|
|
81
|
+
match_statement: { flow: 'nest' }, case_clause: { decision: matchDecisions },
|
|
82
|
+
return_statement: { exit: 'return' }, list_comprehension: { flow: 'sequence' }, set_comprehension: { flow: 'sequence' },
|
|
83
|
+
dictionary_comprehension: { flow: 'sequence' }, generator_expression: { flow: 'sequence' },
|
|
84
|
+
for_in_clause: { flow: 'clause' }, if_clause: { flow: 'filter' },
|
|
85
|
+
},
|
|
86
|
+
parens: new Set(['parenthesized_expression']), comments: new Set(['comment']), extras: new Set(['line_continuation']),
|
|
87
|
+
logicalTypes: new Set(['boolean_operator']), logicalOperators: new Set(['and', 'or']), logicalCognitive: 'sequence',
|
|
88
|
+
parameterSeparators: new Set(['positional_separator', 'keyword_separator']), moduleDepthLabel: 'Module · Control-flow depth', tokens,
|
|
89
|
+
};
|
|
90
|
+
//# sourceMappingURL=python.js.map
|
package/README.md
DELETED