botql 1.1.2 → 1.3.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 +30 -10
- package/Parser.js +5 -1
- package/RAG.js +1 -572
- package/README.md +44 -18
- package/Terminal.js +96 -0
- package/botql.browser.js +248 -526
- package/botql.js +4 -4
- package/createBot.js +88 -0
- package/package.json +5 -4
- package/Bootstrap.js +0 -74
package/LICENSE
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
|
-
MIT
|
|
1
|
+
MIT LICENSE
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
COPYRIGHT (C) 2026 ADILSON C. RAFAEL
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
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
11
|
|
|
12
|
-
|
|
13
|
-
|
|
12
|
+
THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN ALL
|
|
13
|
+
COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE.
|
|
14
14
|
|
|
15
15
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
16
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
@@ -19,3 +19,23 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
19
19
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
20
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
21
|
SOFTWARE.
|
|
22
|
+
|
|
23
|
+
---
|
|
24
|
+
|
|
25
|
+
DUAL LICENSING
|
|
26
|
+
|
|
27
|
+
BOTQL IS AVAILABLE UNDER TWO LICENSES:
|
|
28
|
+
|
|
29
|
+
OPEN SOURCE (MIT) — FREE
|
|
30
|
+
- PERSONAL PROJECTS
|
|
31
|
+
- EDUCATIONAL USE
|
|
32
|
+
- OPEN SOURCE CONTRIBUTIONS
|
|
33
|
+
- NON-COMMERCIAL APPLICATIONS
|
|
34
|
+
|
|
35
|
+
COMMERCIAL LICENSE — PAID
|
|
36
|
+
- ENTERPRISE DEPLOYMENT
|
|
37
|
+
- COMMERCIAL PRODUCTS AND SERVICES
|
|
38
|
+
- PRIORITY SUPPORT
|
|
39
|
+
- ADVANCED CONNECTORS AND FEATURES
|
|
40
|
+
|
|
41
|
+
FOR COMMERCIAL LICENSING, CONTACT: ADILSONRAFAEL847@GMAIL.COM
|
package/Parser.js
CHANGED
|
@@ -1 +1,5 @@
|
|
|
1
|
-
"use strict";const TokenType={KEYWORD:"KEYWORD",STRING:"STRING",NUMBER:"NUMBER",IDENT:"IDENT",SYMBOL:"SYMBOL",EOF:"EOF"},KEYWORDS=new Set(["CREATE","BOT","PLATFORM","CONNECT","TABLE","PREVENT","DEFAULT","ON","START","MESSAGE","FROM","WHEN","CONTAINS","OR","OTHERWISE","KEYWORDS","THINK","WAITING","REPLY","TO","FORWARD","PARSE","SEND","INSERT","INTO","VALUES","UPDATE","SET","WHERE","RUN","IMPORT","AS","RESPONSE"]);class Token{constructor(e,t,s){this.type=e,this.value=t,this.line=s}}class Tokenizer{constructor(e){this.source=e,this.pos=0,this.line=1,this.tokens=[]}error(e){throw Error(`syntax error: ${e}`)}peekChar(e=0){return this.source[this.pos+e]}tokenize(){for(;this.pos<this.source.length;){let e=this.peekChar();if("\n"===e){this.line++,this.pos++;continue}if(/\s/.test(e)){this.pos++;continue}if("-"===e&&"-"===this.peekChar(1)){for(;this.pos<this.source.length&&"\n"!==this.peekChar();)this.pos++;continue}if('"'===e||"'"===e){this.tokens.push(this.readString(e));continue}if(/[0-9]/.test(e)){this.tokens.push(this.readNumber());continue}if("{}(),.=+*;".includes(e)){this.tokens.push(new Token(TokenType.SYMBOL,e,this.line)),this.pos++;continue}if(/[A-Za-z_]/.test(e)){let t=this.readWord();this.tokens.push(t);continue}this.error(`unexpected character: "${e}"`)}return this.tokens.push(new Token(TokenType.EOF,null,this.line)),this.tokens}readString(e){let t=this.line;this.pos++;let s="";for(;this.pos<this.source.length&&this.peekChar()!==e;){if("\\"===this.peekChar()){this.pos++,s+=this.peekChar(),this.pos++;continue}"\n"===this.peekChar()&&this.line++,s+=this.peekChar(),this.pos++}return this.peekChar()!==e&&this.error("unterminated string"),this.pos++,new Token(TokenType.STRING,s,t)}readNumber(){let e=this.line,t="";for(;this.pos<this.source.length&&/[0-9.]/.test(this.peekChar());)t+=this.peekChar(),this.pos++;return new Token(TokenType.NUMBER,Number(t),e)}readWord(){let e=this.line,t="";for(;this.pos<this.source.length&&/[A-Za-z0-9_]/.test(this.peekChar());)t+=this.peekChar(),this.pos++;let s=t.toUpperCase();return KEYWORDS.has(s)&&t===s?new Token(TokenType.KEYWORD,s,e):new Token(TokenType.IDENT,t,e)}}class Parser{constructor(e){this.tokens=new Tokenizer(e).tokenize(),this.pos=0}error(e){let t=this.current();throw Error(`syntax error: ${e}, near "${t?t.value:"EOF"}"`)}current(){return this.tokens[this.pos]}at(e,t){let s=this.current();return s.type===e&&(void 0===t||s.value===t)}atKeyword(...e){return this.at(TokenType.KEYWORD)&&e.includes(this.current().value)}atWord(...e){let t=this.current();return(t.type===TokenType.KEYWORD||t.type===TokenType.IDENT)&&e.includes(t.value)}advance(){let e=this.current();return e.type!==TokenType.EOF&&this.pos++,e}expect(e,t){return this.at(e,t)||this.error(`expected ${t||e}`),this.advance()}expectKeyword(e){return this.expect(TokenType.KEYWORD,e)}isAtEnd(){return this.at(TokenType.EOF)}parseProgram(){let e=[];for(;!this.isAtEnd();)e.push(this.parseStatement());return{type:"Program",body:e}}parseBody(){if(this.at(TokenType.SYMBOL,"{")){this.advance();let e=[];for(;!this.at(TokenType.SYMBOL,"}");)this.isAtEnd()&&this.error('unclosed block "{"'),e.push(this.parseStatement());return this.advance(),e}return[this.parseStatement()]}parseStatement(){return this.atKeyword("CREATE")?this.parseCreate():this.atKeyword("PLATFORM")?this.parsePlatform():this.atKeyword("CONNECT")?this.parseConnect():this.atKeyword("ON")?this.parseOn():this.atKeyword("WHEN")?this.parseWhen():this.atKeyword("OTHERWISE")?this.parseOtherwise():this.atKeyword("REPLY")?this.parseReply():this.atKeyword("THINK")?this.parseThink():this.atKeyword("FORWARD")?this.parseForward():this.atKeyword("PARSE")?this.parseParseSignal():this.atKeyword("SEND")?this.parseSend():this.atKeyword("INSERT")?this.parseInsert():this.atKeyword("UPDATE")?this.parseUpdate():this.atKeyword("RUN")?this.parseRunBot():this.atKeyword("IMPORT")?this.parseImport():void this.error("unexpected statement")}parseCreate(){if(this.expectKeyword("CREATE"),this.atKeyword("BOT")){this.advance();let e=this.expect(TokenType.STRING).value;return{type:"CreateBot",name:e}}if(this.atKeyword("TABLE")){this.advance();let t=this.expect(TokenType.IDENT).value,s=this.parseColumnList(),r=this.tryParsePreventDefault(),i=this.tryParseDefaultMessage();return{type:"CreateTable",name:t,columns:s,preventDefault:r,defaultMessage:i}}this.error("expected BOT or TABLE after CREATE")}parseColumnList(){this.expect(TokenType.SYMBOL,"(");let e=[];for(;!this.at(TokenType.SYMBOL,")");){let t=this.expect(TokenType.IDENT).value,s=[],r=null;for(;this.at(TokenType.IDENT)&&!this.at(TokenType.SYMBOL,",")&&!this.at(TokenType.SYMBOL,")");){let i=this.advance().value;r?s.push(i):r=i}e.push({name:t,columnType:r,constraints:s}),this.at(TokenType.SYMBOL,",")&&this.advance()}return this.expect(TokenType.SYMBOL,")"),e}tryParsePreventDefault(){return!!this.atKeyword("PREVENT")&&(this.advance(),this.expectKeyword("DEFAULT"),!0)}tryParseDefaultMessage(){if(!this.atKeyword("DEFAULT"))return null;let e=this.tokens[this.pos+1];if(!e||e.type!==TokenType.KEYWORD||"MESSAGE"!==e.value)return null;if(this.advance(),this.advance(),this.isReplyFileForm()){this.expect(TokenType.SYMBOL,"(");let t=this.parseFileName();this.expect(TokenType.SYMBOL,",");let s=this.parseExpression();return this.expect(TokenType.SYMBOL,")"),{file:t,index:s}}let r=this.parseExpression();return{value:r}}parsePlatform(){this.expectKeyword("PLATFORM");let e=this.advance().value;return{type:"Platform",value:e}}parseConnect(){if(this.expectKeyword("CONNECT"),this.atKeyword("RESPONSE")){this.advance();let e=this.expect(TokenType.IDENT).value;return{type:"ConnectResponse",alias:e}}let t=this.advance().value,s=this.advance().value,r=this.expect(TokenType.STRING).value;return{type:"Connect",service:t,kind:s,credential:r}}parseOn(){if(this.expectKeyword("ON"),this.atKeyword("START")){this.advance();let e=this.parseBody();return{type:"On",event:"START",body:e}}if(this.atKeyword("MESSAGE")){this.advance();let t=this.parseBody();return{type:"On",event:"MESSAGE",body:t}}if(this.atWord("SIGNAL")){this.advance(),this.expectKeyword("FROM");let s=this.expect(TokenType.STRING).value,r=this.parseBody();return{type:"On",event:"SIGNAL",source:s,body:r}}this.error("unexpected event after ON")}parseWhen(){this.expectKeyword("WHEN");let e=[this.parseCondition()];for(;this.atKeyword("OR");)this.advance(),e.push(this.parseCondition());let t=this.parseBody();return{type:"When",conditions:e,body:t}}parseCondition(){if(this.expectKeyword("CONTAINS"),this.at(TokenType.SYMBOL,"(")){let e=this.parseStringList();return{op:"CONTAINS_ANY",values:e}}if(this.atKeyword("KEYWORDS")){this.advance(),this.expect(TokenType.SYMBOL,"(");let t=this.parseFileName();return this.expect(TokenType.SYMBOL,")"),{op:"CONTAINS_KEYWORDS_FILE",path:t}}let s=this.expect(TokenType.STRING).value;return{op:"CONTAINS",value:s}}parseStringList(){this.expect(TokenType.SYMBOL,"(");let e=[];for(;!this.at(TokenType.SYMBOL,")");)e.push(this.expect(TokenType.STRING).value),this.at(TokenType.SYMBOL,",")&&this.advance();return this.expect(TokenType.SYMBOL,")"),e}parseFileName(){let e=this.expect(TokenType.IDENT).value;for(;this.at(TokenType.SYMBOL,".");)this.advance(),e+="."+this.expect(TokenType.IDENT).value;return e}parseOtherwise(){this.expectKeyword("OTHERWISE");let e=this.parseBody();return{type:"Otherwise",body:e}}parseThink(){this.expectKeyword("THINK"),this.expect(TokenType.SYMBOL,"(");let e=this.parseFileName();this.expect(TokenType.SYMBOL,")");let t=this.tryParseWaiting(),s=null;return this.atKeyword("OR")&&(this.advance(),s=this.parseReply()),{type:"Think",file:e,waiting:t,fallback:s}}tryParseWaiting(){if(!this.atKeyword("WAITING"))return null;this.advance(),this.expect(TokenType.SYMBOL,"(");let e=null,t=null;return!this.at(TokenType.SYMBOL,")")&&(e=this.parseFileName(),this.at(TokenType.SYMBOL,",")&&(this.advance(),t=this.parseExpression())),this.expect(TokenType.SYMBOL,")"),{file:e,seconds:t}}parseReply(){this.expectKeyword("REPLY");let e=null;if(this.atKeyword("TO")&&(this.advance(),e=this.advance().value),this.isReplyFileForm()){this.expect(TokenType.SYMBOL,"(");let t=this.parseFileName();this.expect(TokenType.SYMBOL,",");let s=this.parseExpression();return this.expect(TokenType.SYMBOL,")"),{type:"Reply",target:e,file:t,index:s}}let r=this.parseExpression();return{type:"Reply",target:e,value:r}}isReplyFileForm(){if(!this.at(TokenType.SYMBOL,"("))return!1;let e=this.tokens[this.pos+1],t=this.tokens[this.pos+2];return!!e&&e.type===TokenType.IDENT&&!!t&&t.type===TokenType.SYMBOL&&"."===t.value}parseForward(){this.expectKeyword("FORWARD"),this.expectKeyword("TO");let e=this.parseExpression();return{type:"ForwardTo",target:e}}parseParseSignal(){return this.expectKeyword("PARSE"),this.atWord("SIGNAL")||this.error("expected SIGNAL after PARSE"),this.advance(),{type:"ParseSignal"}}parseSend(){this.expectKeyword("SEND"),this.expectKeyword("TO");let e=this.advance().value;return{type:"SendTo",target:e}}parseInsert(){this.expectKeyword("INSERT"),this.expectKeyword("INTO");let e=this.expect(TokenType.IDENT).value,t=null;this.at(TokenType.SYMBOL,"(")&&(t=this.parseExpressionList());let s=null;return this.atKeyword("VALUES")&&(this.advance(),s=this.parseExpressionList()),{type:"Insert",table:e,columns:t,values:s}}parseExpressionList(){this.expect(TokenType.SYMBOL,"(");let e=[];for(;!this.at(TokenType.SYMBOL,")");)e.push(this.parseExpression()),this.at(TokenType.SYMBOL,",")&&this.advance();return this.expect(TokenType.SYMBOL,")"),e}parseUpdate(){this.expectKeyword("UPDATE");let e=this.expect(TokenType.IDENT).value;this.expectKeyword("SET");let t=this.expect(TokenType.IDENT).value;this.expect(TokenType.SYMBOL,"=");let s=this.parseExpression(),r=null;return this.atKeyword("WHERE")&&(this.advance(),r=this.parseComparison()),{type:"Update",table:e,set:{column:t,value:s},where:r}}parseImport(){this.expectKeyword("IMPORT"),this.expect(TokenType.SYMBOL,"{");let e=this.parseFileName(),t=null;this.at(TokenType.SYMBOL,",")&&(this.advance(),t=this.parseExpression()),this.expect(TokenType.SYMBOL,"}");let s=null;return this.atKeyword("AS")&&(this.advance(),s=this.expect(TokenType.IDENT).value),{type:"Import",path:e,index:t,alias:s}}parseRunBot(){return this.expectKeyword("RUN"),this.expectKeyword("BOT"),{type:"RunBot"}}parseExpression(){let e=this.parsePrimary();for(;this.at(TokenType.SYMBOL,"+");){this.advance();let t=this.parsePrimary();e={type:"Binary",op:"+",left:e,right:t}}return e}parseComparison(){let e=this.parseExpression();if(this.at(TokenType.SYMBOL,"=")){this.advance();let t=this.parseExpression();return{type:"Binary",op:"=",left:e,right:t}}return e}parsePrimary(){let e=this.current();if(e.type===TokenType.STRING||e.type===TokenType.NUMBER)return this.advance(),{type:"Literal",value:e.value};if(e.type===TokenType.IDENT){this.advance();let t={type:"Identifier",name:e.value};if(this.at(TokenType.SYMBOL,"(")){let s=this.parseExpressionList();t={type:"Call",callee:t.name,args:s}}for(;this.at(TokenType.SYMBOL,".");){this.advance();let r=this.expect(TokenType.IDENT).value;t={type:"Member",object:t,property:r}}return t}this.error("invalid expression")}}module.exports={Parser,Tokenizer,TokenType,KEYWORDS};
|
|
1
|
+
/**
|
|
2
|
+
*Parser.min.js
|
|
3
|
+
*
|
|
4
|
+
*/
|
|
5
|
+
"use strict";const TokenType={KEYWORD:"KEYWORD",STRING:"STRING",NUMBER:"NUMBER",IDENT:"IDENT",SYMBOL:"SYMBOL",EOF:"EOF"},KEYWORDS=new Set(["CREATE","BOT","PLATFORM","CONNECT","TABLE","PREVENT","DEFAULT","ON","START","MESSAGE","FROM","WHEN","CONTAINS","OR","OTHERWISE","KEYWORDS","THINK","WAITING","REPLY","TO","FORWARD","PARSE","SEND","INSERT","INTO","VALUES","UPDATE","SET","WHERE","RUN","IMPORT","AS","RESPONSE"]);class Token{constructor(e,t,s,r,i){this.type=e,this.value=t,this.line=s,this.start=r,this.end=i}}class BotQLSyntaxError extends Error{constructor(e,{line:t=null,start:s=0,end:r=null,near:i=null}={}){super(e),this.name="BotQLSyntaxError",this.line=t,this.start=s,this.end=null===r?s+1:r,this.near=i}}class Tokenizer{constructor(e){this.source=e,this.pos=0,this.line=1,this.tokens=[]}error(e,t,s){let r=void 0!==t?t:this.pos;throw new BotQLSyntaxError(`syntax error: ${e}`,{line:this.line,start:r,end:s})}peekChar(e=0){return this.source[this.pos+e]}tokenize(){for(;this.pos<this.source.length;){let e=this.peekChar(),t=this.pos;if("\n"===e){this.line++,this.pos++;continue}if(/\s/.test(e)){this.pos++;continue}if("-"===e&&"-"===this.peekChar(1)){for(;this.pos<this.source.length&&"\n"!==this.peekChar();)this.pos++;continue}let s;'"'===e||"'"===e?s=this.readString(e):/[0-9]/.test(e)?s=this.readNumber():"{}(),.=+*;".includes(e)?(s=new Token(TokenType.SYMBOL,e,this.line),this.pos++):/[A-Za-z_]/.test(e)?s=this.readWord():this.error(`unexpected character: "${e}"`,t,t+1),s.start=t,s.end=this.pos,this.tokens.push(s)}let r=this.source.length,i=new Token(TokenType.EOF,null,this.line,r,r);return this.tokens.push(i),this.tokens}readString(e){let t=this.line,s=this.pos;this.pos++;let r="";for(;this.pos<this.source.length&&this.peekChar()!==e;){if("\\"===this.peekChar()){this.pos++,r+=this.peekChar(),this.pos++;continue}"\n"===this.peekChar()&&this.line++,r+=this.peekChar(),this.pos++}return this.peekChar()!==e&&this.error("unterminated string",s,this.pos),this.pos++,new Token(TokenType.STRING,r,t)}readNumber(){let e=this.line,t="";for(;this.pos<this.source.length&&/[0-9.]/.test(this.peekChar());)t+=this.peekChar(),this.pos++;return new Token(TokenType.NUMBER,Number(t),e)}readWord(){let e=this.line,t="";for(;this.pos<this.source.length&&/[A-Za-z0-9_]/.test(this.peekChar());)t+=this.peekChar(),this.pos++;let s=t.toUpperCase();return KEYWORDS.has(s)&&t===s?new Token(TokenType.KEYWORD,s,e):new Token(TokenType.IDENT,t,e)}}class Parser{constructor(e){this.source=e,this.tokens=new Tokenizer(e).tokenize(),this.pos=0}error(e){let t=this.current(),s=t&&t.type!==TokenType.EOF?t.value:"EOF",r=t?t.start:this.source.length,i=t?t.end:this.source.length;throw new BotQLSyntaxError(`syntax error: ${e}, near "${s}"`,{line:t?t.line:null,start:r,end:i,near:s})}current(){return this.tokens[this.pos]}at(e,t){let s=this.current();return s.type===e&&(void 0===t||s.value===t)}atKeyword(...e){return this.at(TokenType.KEYWORD)&&e.includes(this.current().value)}atWord(...e){let t=this.current();return(t.type===TokenType.KEYWORD||t.type===TokenType.IDENT)&&e.includes(t.value)}advance(){let e=this.current();return e.type!==TokenType.EOF&&this.pos++,e}expect(e,t){return this.at(e,t)||this.error(`expected ${t||e}`),this.advance()}expectKeyword(e){return this.expect(TokenType.KEYWORD,e)}isAtEnd(){return this.at(TokenType.EOF)}parseProgram(){let e=[];for(;!this.isAtEnd();)e.push(this.parseStatement());return{type:"Program",body:e}}parseBody(){if(this.at(TokenType.SYMBOL,"{")){this.advance();let e=[];for(;!this.at(TokenType.SYMBOL,"}");)this.isAtEnd()&&this.error('unclosed block "{"'),e.push(this.parseStatement());return this.advance(),e}return[this.parseStatement()]}parseStatement(){return this.atKeyword("CREATE")?this.parseCreate():this.atKeyword("PLATFORM")?this.parsePlatform():this.atKeyword("CONNECT")?this.parseConnect():this.atKeyword("ON")?this.parseOn():this.atKeyword("WHEN")?this.parseWhen():this.atKeyword("OTHERWISE")?this.parseOtherwise():this.atKeyword("REPLY")?this.parseReply():this.atKeyword("THINK")?this.parseThink():this.atKeyword("WAITING")?this.parseWaitingStatement():this.atKeyword("FORWARD")?this.parseForward():this.atKeyword("PARSE")?this.parseParseSignal():this.atKeyword("SEND")?this.parseSend():this.atKeyword("INSERT")?this.parseInsert():this.atKeyword("UPDATE")?this.parseUpdate():this.atKeyword("RUN")?this.parseRunBot():this.atKeyword("IMPORT")?this.parseImport():void this.error("unexpected statement")}parseCreate(){if(this.expectKeyword("CREATE"),this.atKeyword("BOT")){this.advance();let e=this.expect(TokenType.STRING).value;return{type:"CreateBot",name:e}}if(this.atKeyword("TABLE")){this.advance();let t=this.expect(TokenType.IDENT).value,s=this.parseColumnList(),r=this.tryParsePreventDefault(),i=this.tryParseDefaultMessage();return{type:"CreateTable",name:t,columns:s,preventDefault:r,defaultMessage:i}}this.error("expected BOT or TABLE after CREATE")}parseColumnList(){this.expect(TokenType.SYMBOL,"(");let e=[];for(;!this.at(TokenType.SYMBOL,")");){let t=this.expect(TokenType.IDENT).value,s=[],r=null;for(;this.at(TokenType.IDENT)&&!this.at(TokenType.SYMBOL,",")&&!this.at(TokenType.SYMBOL,")");){let i=this.advance().value;r?s.push(i):r=i}e.push({name:t,columnType:r,constraints:s}),this.at(TokenType.SYMBOL,",")&&this.advance()}return this.expect(TokenType.SYMBOL,")"),e}tryParsePreventDefault(){return!!this.atKeyword("PREVENT")&&(this.advance(),this.expectKeyword("DEFAULT"),!0)}tryParseDefaultMessage(){if(!this.atKeyword("DEFAULT"))return null;let e=this.tokens[this.pos+1];if(!e||e.type!==TokenType.KEYWORD||"MESSAGE"!==e.value)return null;if(this.advance(),this.advance(),this.isReplyFileForm()){this.expect(TokenType.SYMBOL,"(");let t=this.parseFileName();this.expect(TokenType.SYMBOL,",");let s=this.parseExpression();return this.expect(TokenType.SYMBOL,")"),{file:t,index:s}}let r=this.parseExpression();return{value:r}}parsePlatform(){this.expectKeyword("PLATFORM");let e=this.advance().value;return{type:"Platform",value:e}}parseConnect(){if(this.expectKeyword("CONNECT"),this.atKeyword("RESPONSE")){this.advance();let e=this.expect(TokenType.IDENT).value;return{type:"ConnectResponse",alias:e}}let t=this.advance().value,s=this.advance().value,r=this.expect(TokenType.STRING).value;return{type:"Connect",service:t,kind:s,credential:r}}parseOn(){if(this.expectKeyword("ON"),this.atKeyword("START")){this.advance();let e=this.parseBody();return{type:"On",event:"START",body:e}}if(this.atKeyword("MESSAGE")){this.advance();let t=this.parseBody();return{type:"On",event:"MESSAGE",body:t}}if(this.atWord("SIGNAL")){this.advance(),this.expectKeyword("FROM");let s=this.expect(TokenType.STRING).value,r=this.parseBody();return{type:"On",event:"SIGNAL",source:s,body:r}}this.error("unexpected event after ON")}parseWhen(){this.expectKeyword("WHEN");let e=[this.parseCondition()];for(;this.atKeyword("OR");)this.advance(),e.push(this.parseCondition());let t=this.parseBody();return{type:"When",conditions:e,body:t}}parseCondition(){if(this.expectKeyword("CONTAINS"),this.at(TokenType.SYMBOL,"(")){let e=this.parseStringList();return{op:"CONTAINS_ANY",values:e}}if(this.atKeyword("KEYWORDS")){this.advance(),this.expect(TokenType.SYMBOL,"(");let t=this.parseFileName();return this.expect(TokenType.SYMBOL,")"),{op:"CONTAINS_KEYWORDS_FILE",path:t}}let s=this.expect(TokenType.STRING).value;return{op:"CONTAINS",value:s}}parseStringList(){this.expect(TokenType.SYMBOL,"(");let e=[];for(;!this.at(TokenType.SYMBOL,")");)e.push(this.expect(TokenType.STRING).value),this.at(TokenType.SYMBOL,",")&&this.advance();return this.expect(TokenType.SYMBOL,")"),e}parseFileName(){let e=this.expect(TokenType.IDENT).value;for(;this.at(TokenType.SYMBOL,".");)this.advance(),e+="."+this.expect(TokenType.IDENT).value;return e}parseOtherwise(){this.expectKeyword("OTHERWISE");let e=this.parseBody();return{type:"Otherwise",body:e}}parseThink(){this.expectKeyword("THINK"),this.expect(TokenType.SYMBOL,"(");let e=this.parseFileName();this.expect(TokenType.SYMBOL,")");let t=this.tryParseWaiting(),s=null;return this.atKeyword("OR")&&(this.advance(),s=this.parseReply()),{type:"Think",file:e,waiting:t,fallback:s}}tryParseWaiting(){if(!this.atKeyword("WAITING"))return null;this.advance(),this.expect(TokenType.SYMBOL,"(");let e=null,t=null,s=null;return!this.at(TokenType.SYMBOL,")")&&(this.at(TokenType.STRING)?t=this.advance().value:e=this.parseFileName(),this.at(TokenType.SYMBOL,",")&&(this.advance(),s=this.parseExpression())),this.expect(TokenType.SYMBOL,")"),{file:e,text:t,seconds:s}}parseWaitingStatement(){let e=this.tryParseWaiting();return{type:"Waiting",file:e.file,text:e.text,seconds:e.seconds}}parseReply(){this.expectKeyword("REPLY");let e=null;if(this.atKeyword("TO")&&(this.advance(),e=this.advance().value),this.isReplyFileForm()){this.expect(TokenType.SYMBOL,"(");let t=this.parseFileName();this.expect(TokenType.SYMBOL,",");let s=this.parseExpression();return this.expect(TokenType.SYMBOL,")"),{type:"Reply",target:e,file:t,index:s}}let r=this.parseExpression();return{type:"Reply",target:e,value:r}}isReplyFileForm(){if(!this.at(TokenType.SYMBOL,"("))return!1;let e=this.tokens[this.pos+1],t=this.tokens[this.pos+2];return!!e&&e.type===TokenType.IDENT&&!!t&&t.type===TokenType.SYMBOL&&"."===t.value}parseForward(){this.expectKeyword("FORWARD"),this.expectKeyword("TO");let e=this.parseExpression();return{type:"ForwardTo",target:e}}parseParseSignal(){return this.expectKeyword("PARSE"),this.atWord("SIGNAL")||this.error("expected SIGNAL after PARSE"),this.advance(),{type:"ParseSignal"}}parseSend(){this.expectKeyword("SEND"),this.expectKeyword("TO");let e=this.advance().value;return{type:"SendTo",target:e}}parseInsert(){this.expectKeyword("INSERT"),this.expectKeyword("INTO");let e=this.expect(TokenType.IDENT).value,t=null;this.at(TokenType.SYMBOL,"(")&&(t=this.parseExpressionList());let s=null;return this.atKeyword("VALUES")&&(this.advance(),s=this.parseExpressionList()),{type:"Insert",table:e,columns:t,values:s}}parseExpressionList(){this.expect(TokenType.SYMBOL,"(");let e=[];for(;!this.at(TokenType.SYMBOL,")");)e.push(this.parseExpression()),this.at(TokenType.SYMBOL,",")&&this.advance();return this.expect(TokenType.SYMBOL,")"),e}parseUpdate(){this.expectKeyword("UPDATE");let e=this.expect(TokenType.IDENT).value;this.expectKeyword("SET");let t=this.expect(TokenType.IDENT).value;this.expect(TokenType.SYMBOL,"=");let s=this.parseExpression(),r=null;return this.atKeyword("WHERE")&&(this.advance(),r=this.parseComparison()),{type:"Update",table:e,set:{column:t,value:s},where:r}}parseImport(){this.expectKeyword("IMPORT"),this.expect(TokenType.SYMBOL,"{");let e=this.parseFileName(),t=null;this.at(TokenType.SYMBOL,",")&&(this.advance(),t=this.parseExpression()),this.expect(TokenType.SYMBOL,"}");let s=null;return this.atKeyword("AS")&&(this.advance(),s=this.expect(TokenType.IDENT).value),{type:"Import",path:e,index:t,alias:s}}parseRunBot(){return this.expectKeyword("RUN"),this.expectKeyword("BOT"),{type:"RunBot"}}parseExpression(){let e=this.parsePrimary();for(;this.at(TokenType.SYMBOL,"+");){this.advance();let t=this.parsePrimary();e={type:"Binary",op:"+",left:e,right:t}}return e}parseComparison(){let e=this.parseExpression();if(this.at(TokenType.SYMBOL,"=")){this.advance();let t=this.parseExpression();return{type:"Binary",op:"=",left:e,right:t}}return e}parsePrimary(){let e=this.current();if(e.type===TokenType.STRING||e.type===TokenType.NUMBER)return this.advance(),{type:"Literal",value:e.value};if(e.type===TokenType.IDENT){this.advance();let t={type:"Identifier",name:e.value};if(this.at(TokenType.SYMBOL,"(")){let s=this.parseExpressionList();t={type:"Call",callee:t.name,args:s}}for(;this.at(TokenType.SYMBOL,".");){this.advance();let r=this.expect(TokenType.IDENT).value;t={type:"Member",object:t,property:r}}return t}this.error("invalid expression")}}module.exports={Parser,Tokenizer,TokenType,KEYWORDS,BotQLSyntaxError};
|