Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

脚本支持多语言(python、sql)模式开发 #10

Open
bebee4java opened this issue Feb 1, 2021 · 2 comments · Fixed by #11
Open

脚本支持多语言(python、sql)模式开发 #10

bebee4java opened this issue Feb 1, 2021 · 2 comments · Fixed by #11
Labels
feature 新特性

Comments

@bebee4java
Copy link
Owner

ides自定义了一整套dsl,如(load/save/set/connect...)等语法,具体参考
我们希望从语法层原生支持sqlpython等语言的执行,这样可以丰富脚本开发,因为很有可能某些同学习惯使用sqlpython进行数据处理(算法同学同样需要python加持)。

@bebee4java
Copy link
Owner Author

上面的需求需要在一门脚本语言上支持多种语言开发。我们可以参考Zeppelin的做法,使用不同的Interpreter,大概是这样:

%python
a=1
print(1)
%

对于支持jdbc协议(mysql/kylin)的sql像这样:

%sql(test)
select 1 from test;
% > output

值得关注
我们和Zeppelin有少许差异:

  1. 末尾需要用%标识代码结束
  2. 如果执行器需要特殊输入可以通过()引用。如python需要对a表做处理:%python(a);sql需要使用test连接:%sql(test)。这和ZeppelinKylin解释器类似。
  3. 如果代码执行完需要输出可以用> output指定

@bebee4java bebee4java added enhancement New feature or request feature 新特性 and removed enhancement New feature or request labels Feb 1, 2021
@bebee4java
Copy link
Owner Author

bebee4java commented Feb 1, 2021

我们通过antlr lexical modes找到解决方法,antler的词法分析模式允许在同份文件中包含多重语言。通过一些特殊的"哨兵"字符序列,执行不同模式的切换,参考文档
官方提供了xml语言实现lexical modes的例子,参考代码

我们的实现差不多像这样:
定义IdesLexer.g4文件:

lexer grammar IdesLexer;
...

PY_MODE : '%python' -> pushMode(PYTHON_LAN);
SQL_MODE : '%sql' -> pushMode(SQL_LAN);

...

mode PYTHON_LAN;

EXIT_PY : '%' -> popMode;
IGNORE_PY : . -> more ;
...

mode SQL_LAN;

EXIT_SQL : '%' -> popMode;
IGNORE_SQL : . -> more ;
...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature 新特性
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant