@instrumenta/renpy 0.0.0-stage → 0.0.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ Copyright 2026 pizcadesaber
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,20 @@
1
- # Temporary Holding Version
1
+ # Renpy Parser
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Parse `.rpy` files:
4
+
5
+ ```ts
6
+ import {parse} from "@instrumenta/renpy";
7
+
8
+ const FILENAME = "script.rpy";
9
+ const source = await readFile(FILENAME, "utf8");
10
+
11
+ const result = parse(source, FILENAME);
12
+
13
+ for (const node of result.body) {
14
+ switch (node.type) {
15
+ case "label":
16
+ console.log("label:", node.name);
17
+ break;
18
+ }
19
+ }
20
+ ```
@@ -0,0 +1,94 @@
1
+ import { PythonExpression, PythonModule, PythonStatement } from "@instrumenta/python";
2
+ import { Lexer, Parser, Token } from "@pizca/ast";
3
+ interface RenpyFile {
4
+ type: "file";
5
+ filename: string;
6
+ body: RenpyNode[];
7
+ }
8
+ type RenpyNode = DefineNode | DefaultNode | LabelNode | SayNode | JumpNode | CallNode | ReturnNode | MenuNode | IfNode | PythonBlockNode | PythonInlineNode | CommandNode;
9
+ interface DefineNode {
10
+ type: "define";
11
+ name: string;
12
+ expression: string;
13
+ expressionAst: PythonExpression;
14
+ }
15
+ interface DefaultNode {
16
+ type: "default";
17
+ name: string;
18
+ expression: string;
19
+ expressionAst: PythonExpression;
20
+ }
21
+ interface LabelNode {
22
+ type: "label";
23
+ name: string;
24
+ body: RenpyNode[];
25
+ }
26
+ interface SayNode {
27
+ type: "say";
28
+ speaker: string | null;
29
+ text: string;
30
+ }
31
+ interface JumpNode {
32
+ type: "jump";
33
+ target: string;
34
+ }
35
+ interface CallNode {
36
+ type: "call";
37
+ target: string;
38
+ from?: string;
39
+ }
40
+ interface ReturnNode {
41
+ type: "return";
42
+ }
43
+ interface MenuNode {
44
+ type: "menu";
45
+ items: MenuItem[];
46
+ /** Optional transition applied when entering the menu. */
47
+ transition?: string;
48
+ }
49
+ interface MenuItem {
50
+ text: string;
51
+ condition?: string;
52
+ body: RenpyNode[];
53
+ }
54
+ interface IfNode {
55
+ type: "if";
56
+ branches: IfBranch[];
57
+ }
58
+ interface IfBranch {
59
+ condition: string | null;
60
+ expressionAst?: PythonExpression;
61
+ body: RenpyNode[];
62
+ }
63
+ interface PythonBlockNode {
64
+ type: "python-block";
65
+ body: PythonModule;
66
+ }
67
+ interface PythonInlineNode {
68
+ type: "python-inline";
69
+ statement: PythonStatement;
70
+ }
71
+ /**
72
+ * A Ren'Py statement whose arguments are not interpreted by this parser.
73
+ *
74
+ * Keeping the original source text makes commands such as audio, scene,
75
+ * show/hide, transitions and many screen-related statements lossless while
76
+ * the parser incrementally gains dedicated AST nodes for them.
77
+ */
78
+ interface CommandNode {
79
+ type: "command";
80
+ name: string;
81
+ arguments: string;
82
+ /** Raw indented block for commands that support ATL-style suites. */
83
+ block?: string;
84
+ }
85
+ export declare class RenpyLexer extends Lexer {
86
+ #private;
87
+ tokenize(): readonly Token[];
88
+ }
89
+ export declare class RenpyParser extends Parser {
90
+ #private;
91
+ parse(): RenpyFile;
92
+ }
93
+ export declare function parse(text: string, filename?: string): RenpyFile;
94
+ export type { CallNode, CommandNode, DefaultNode, DefineNode, IfBranch, IfNode, JumpNode, LabelNode, MenuItem, MenuNode, PythonBlockNode, PythonInlineNode, RenpyFile, RenpyNode, ReturnNode, SayNode };
package/dist/index.js ADDED
@@ -0,0 +1,9 @@
1
+ import{Lexer as e,Parser as t,Source as n}from"@pizca/ast";import{parseExpressionRange as r,parseRange as i}from"@instrumenta/python";const a=/* @__PURE__ */ `===.!==.**=.//=.<<=.>>=.==.!=.<=.>=.+=.-=.*=./=.%=.@=.//.**.<<.>>.+.-.*./.%.<.>.!.~.&.|.^.@.=.$`.split(`.`);var o=class t extends e{#e=[0];#t=!0;tokenize(){for(;!this.eof;){if(this.#t){if(this.#n(),this.eof)break;if(this.peek()===`
2
+ `||this.peek()===`\r`){this.#r();continue}if(this.peek()===`#`){this.#i();continue}}let e=this.peek();e===` `||e===` `?this.advance():e===`\r`||e===`
3
+ `?this.#r():e===`#`?this.#i():e===`"`||e===`'`?this.#a():this.#d(e)?this.#o():this.#p(e)?this.#s():this.#c()||this.#l()||this.error(`RENPY_UNKNOWN_CHARACTER`,`Unsupported character: ${JSON.stringify(e)}`,this.position,this.position+1)}for(this.tokens.length>0&&this.tokens[this.tokens.length-1]?.type!==16&&this.emit(16,this.position);this.#e.length>1;)this.#e.pop(),this.emit(15,this.position);return this.emit(17,this.position),this.tokens}#n(){for(;!this.eof;){let e=this.position,t=0;for(;!this.eof;){let e=this.peek();if(e===` `)t++,this.advance();else if(e===` `)t+=8-t%8,this.advance();else break}if(this.peek()===`#`){this.#i(),(this.peek()===`
4
+ `||this.peek()===`\r`)&&this.#r();continue}if(this.peek()===`
5
+ `||this.peek()===`\r`){this.#r();continue}if(this.eof)return;this.#t=!1;let n=this.#e[this.#e.length-1];if(t>n){this.#e.push(t),this.emit(14,e);return}if(t<n){for(;this.#e.length>1&&t<this.#e[this.#e.length-1];)this.#e.pop(),this.emit(15,e);t!==this.#e[this.#e.length-1]&&this.error(`RENPY_INVALID_INDENTATION`,`Indentation does not match a previous block.`,e,this.position)}return}}#r(){let e=this.position;this.peek()===`\r`?(this.advance(),this.peek()===`
6
+ `&&this.advance()):this.advance(),this.#t=!0,this.emit(16,e)}#i(){for(;!this.eof&&this.peek()!==`
7
+ `&&this.peek()!==`\r`;)this.advance()}#a(){let e=this.position,t=this.peek(),n=this.source.text.startsWith(t.repeat(3),e);for(this.advance(),n&&(this.advance(),this.advance());!this.eof;){if(n&&this.source.text.startsWith(t.repeat(3),this.position)){this.advance(),this.advance(),this.advance(),this.emit(2,e);return}let r=this.advance();if(r===`\\`)this.eof||this.advance();else{if(!n&&r===t){this.emit(2,e);return}n||r!==`
8
+ `&&r!==`\r`||this.error(`RENPY_MULTILINE_STRING_UNSUPPORTED`,`Multiline strings are not supported yet.`,e,this.position)}}this.error(`RENPY_UNTERMINATED_STRING`,`Unterminated string.`,e,this.position)}#o(){let e=this.position;for(this.advance();this.#f(this.peek());)this.advance();this.emit(1,e)}#s(){let e=this.position;for(;this.#p(this.peek());)this.advance();if(this.peek()===`.`&&this.#p(this.peek(1)))for(this.advance();this.#p(this.peek());)this.advance();this.emit(3,e)}#c(){let e=this.position,n=this.peek(),r=t.#g[n];return r?(this.advance(),this.emit(r,e),!0):!1}#l(){for(let e of a)if(this.source.text.startsWith(e,this.position)){let t=this.position;return this.#u(e),this.emit(e===`$`?18:4,t),!0}return!1}#u(e){for(let t=0;t<e.length;t++)this.advance()}#d(e){return t.#m.test(e)}#f(e){return t.#h.test(e)}#p(e){return e>=`0`&&e<=`9`}static#m=/[a-zA-Z_]/;static#h=/[a-zA-Z0-9_]/;static#g={":":5,",":6,";":19,".":7,"(":8,")":9,"[":10,"]":11,"{":12,"}":13}},s=class extends t{parse(){let e=this.#e(!1);return this.expect(17),{type:`file`,filename:this.source.filename,body:e}}#e(e){let t=[];for(;!this.eof;){if(this.match(16))continue;let n=this.peek();if(!n||n.type===17)break;if(n.type===15){if(e)break;this.error(`RENPY_UNEXPECTED_DEDENT`,`Unexpected indentation block termination.`)}n.type===14&&this.error(`RENPY_UNEXPECTED_INDENT`,`Unexpected indentation at statement level.`),t.push(this.#t())}return t}#t(){let e=this.#g();if(e.length===0&&this.#C(e,`Expected a statement.`),e[0]?.type===18){let t=i(this.source,e[0].end,e[e.length-1].end);return t.body.length!==1&&this.#C(e,`Expected one inline Python statement.`),{type:`python-inline`,statement:t.body[0]}}let t=this.#b(e[0]);switch(t){case`define`:case`default`:return this.#n(e,t);case`label`:return this.#r(e);case`jump`:return this.#i(e);case`call`:return this.#a(e);case`return`:return this.#o(e);case`menu`:return this.#u(e);case`if`:return this.#d(e);case`python`:return this.#f(e);default:return this.#s(e)}}#n(e,t){(e.length<4||e[1]?.type!==1)&&this.#C(e,`Invalid ${t} declaration.`);let n=2;for(;e[n]?.type===7&&e[n+1]?.type===1;)n+=2;(e[n]?.type!==4||this.#b(e[n])!==`=`)&&this.#C(e,`Invalid ${t} declaration.`);let r=n+1,i=this.#x(e,r);return i&&e[r]||this.#C(e,`${t} requires an expression.`),{type:t,name:e.slice(1,n).map(e=>this.#b(e)).join(``),expression:i,expressionAst:this.#y(e[r].start,e[e.length-1].end)}}#r(e){return(e.length!==3||e[1]?.type!==1||e[2]?.type!==5)&&this.#C(e,`Invalid label declaration.`),{type:`label`,name:this.#b(e[1]),body:this.#m()}}#i(e){return(e.length!==2||e[1]?.type!==1)&&this.#C(e,`Invalid jump statement.`),{type:`jump`,target:this.#b(e[1])}}#a(e){e[1]?.type!==1&&this.#C(e,`Invalid call statement.`);let t=this.#b(e[1]);if(e.length===2)return{type:`call`,target:t};if(e.length===4&&e[2]?.type===1&&this.#b(e[2])===`from`&&e[3]?.type===1)return{type:`call`,target:t,from:this.#b(e[3])};this.#C(e,`Invalid call statement.`)}#o(e){return e.length!==1&&this.#C(e,`Return with an expression is not supported yet.`),{type:`return`}}#s(e){if(e.length===1&&e[0]?.type===2||e.length===2&&e[0]?.type===1&&e[1]?.type===2)return this.#l(e);let t=e[0];t&&t.type===1||this.#C(e,`Unknown Ren'Py statement or unsupported dialogue syntax.`);let n=e[e.length-1]?.type===5?this.#c():void 0;return{type:`command`,name:this.#b(t),arguments:this.#x(e,1),...n===void 0?{}:{block:n}}}#c(){this.#h();let e=this.#_(),t=0,n=e.start;for(;!this.eof;){let e=this.peek();if(!e||e.type===17)break;if(e.type===14)t++;else if(e.type===15){if(t===0){n=e.start,this.consume();break}t--}n=e.end,this.consume()}return this.source.slice(e.start,n)}#l(e){let t=null,n;return e.length===1&&e[0]?.type===2?n=this.#S(e[0]):e.length===2&&e[0]?.type===1&&e[1]?.type===2?(t=this.#b(e[0]),n=this.#S(e[1])):this.#C(e,`Unknown Ren'Py statement or unsupported dialogue syntax.`),{type:`say`,speaker:t,text:n}}#u(e){(e.length!==2||e[1]?.type!==5)&&this.#C(e,`Invalid menu declaration.`),this.#h(),this.#_();let t=[],n;for(;!this.eof&&this.peek()?.type!==15;){if(this.match(16))continue;let e=this.#g();if(e[0]?.type===1&&this.#b(e[0])===`with`){(n!==void 0||e.length<2)&&this.#C(e,`Invalid menu transition.`),n=this.#x(e,1);continue}let r=e[e.length-1];(e.length<2||e[0]?.type!==2||r?.type!==5)&&this.#C(e,`Expected a menu option with a string and colon.`);let i;if(e.length>2){(e[1]?.type!==1||this.#b(e[1])!==`if`||e.length<4)&&this.#C(e,`Invalid menu option condition.`);let t=e[2],n=e[e.length-2];i=this.source.slice(t.start,n.end),this.#y(t.start,n.end)}t.push({text:this.#S(e[0]),...i===void 0?{}:{condition:i},body:this.#m()})}this.#v(),t.length===0&&this.#C(e,`Menu must contain at least one option.`);let r={type:`menu`,items:t};return n&&(r.transition=n),r}#d(e){let t=this.#p(e,`if`),n=[{condition:t.text,expressionAst:t.ast,body:this.#m()}];for(;!this.eof;){if(this.peek()?.type===16){this.consume();continue}if(this.peek()?.type!==1)break;let e=this.checkpoint(),t=this.#g(),r=this.#b(t[0]);if(r!==`elif`&&r!==`else`){this.restore(e);break}if(r===`else`){(t.length!==2||t[1]?.type!==5)&&this.#C(t,`Invalid else clause.`),n.push({condition:null,body:this.#m()});break}let i=this.#p(t,`elif`);n.push({condition:i.text,expressionAst:i.ast,body:this.#m()})}return{type:`if`,branches:n}}#f(e){(e.length!==2||e[1]?.type!==5)&&this.#C(e,`Expected 'python:' block.`),this.#h();let t=this.#_().start,n=0,r=t;for(;!this.eof;){let e=this.peek();if(!e||e.type===17)break;if(e.type===14)n++;else if(e.type===15){if(n===0){r=e.start,this.consume();break}n--}r=e.end,this.consume()}r<=t&&this.#C(e,`Python block cannot be empty.`);let a=i(this.source,t,r);return a.body.length===0&&this.#C(e,`Python block cannot be empty.`),{type:`python-block`,body:a}}#p(e,t){(e.length<3||this.#b(e[0])!==t||e[e.length-1]?.type!==5)&&this.#C(e,`Invalid ${t} condition.`);let n=e[1],r=e[e.length-2];return{text:this.source.slice(n.start,r.end),ast:this.#y(n.start,r.end)}}#m(){this.#h(),this.#_();let e=this.#e(!0);return this.#v(),e.length===0&&this.error(`RENPY_EMPTY_BLOCK`,`Expected at least one statement in the block.`),e}#h(){for(;this.match(16););}#g(){let e=[];for(;!this.eof;){let t=this.peek();if(!t||t.type===16||t.type===17||t.type===15)break;e.push(this.consume())}return this.peek()?.type===16&&this.consume(),e.length===0&&!this.eof&&this.error(`RENPY_EXPECTED_STATEMENT`,`Expected a statement.`),e}#_(){for(;this.match(16););return this.expect(14,`Expected an indented block.`)}#v(){this.expect(15,`Expected the end of the indented block.`)}#y(e,t){return r(this.source,e,t)}#b(e){return this.source.slice(e.start,e.end)}#x(e,t,n=e.length){let r=e[t],i=e[n-1];return r&&i?this.source.slice(r.start,i.end):``}#S(e){let t=this.#b(e),n=t[0];n||this.error(`RENPY_INVALID_STRING`,`Invalid string token.`,e.start,e.end);let r=t.startsWith(n.repeat(3))?3:1;return(t.length<r*2||!t.endsWith(n.repeat(r)))&&this.error(`RENPY_INVALID_STRING`,`Invalid string token.`,e.start,e.end),t.slice(r,-r).replace(/\\([\\'"nrt])/g,(e,t)=>{switch(t){case`n`:return`
9
+ `;case`r`:return`\r`;case`t`:return` `;default:return t}})}#C(e,t){let n=e[0],r=e[e.length-1],i=n?.start??this.source.length,a=r?.end??i;this.error(`RENPY_UNSUPPORTED_SYNTAX`,`${t} Source: ${this.source.slice(i,a)}`,i,a)}};function c(e,t=`<input>`){let r=new n(e,t);return new s(r,new o(r).tokenize()).parse()}export{o as RenpyLexer,s as RenpyParser,c as parse};
package/package.json CHANGED
@@ -1,6 +1,37 @@
1
1
  {
2
- "name": "@instrumenta/renpy",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
2
+ "name": "@instrumenta/renpy",
3
+ "version": "0.0.1",
4
+ "description": "Renpy parser.",
5
+ "license": "MIT",
6
+ "author": "pizca",
7
+ "type": "module",
8
+ "sideEffects": false,
9
+ "files": [
10
+ "dist"
11
+ ],
12
+ "exports": {
13
+ ".": {
14
+ "import": "./dist/index.js",
15
+ "types": "./dist/index.d.ts"
16
+ }
17
+ },
18
+ "scripts": {
19
+ "build": "tsc && tsdown",
20
+ "test": "tsx test --coverage",
21
+ "prewatch": "tsdown --watch &",
22
+ "watch": "tsc --watch --noEmit",
23
+ "prepublishOnly": "tsdown --tsconfig=tsconfig.prod.json"
24
+ },
25
+ "dependencies": {
26
+ "@instrumenta/python": "^0.0.1",
27
+ "@pizca/ast": "^0.0.1"
28
+ },
29
+ "keywords": [
30
+ "ast",
31
+ "python",
32
+ "renpy"
33
+ ],
34
+ "engines": {
35
+ "node": ">=26"
36
+ }
6
37
  }