Skip to content

Commit

Permalink
Implement parser for FFI calls
Browse files Browse the repository at this point in the history
  • Loading branch information
klntsky committed Nov 22, 2024
1 parent 6188f81 commit 04bec7b
Show file tree
Hide file tree
Showing 11 changed files with 391 additions and 235 deletions.
11 changes: 9 additions & 2 deletions grammar/MetaPrompt.g4
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@ meta_body
: IF_KW exprs THEN_KW exprs ELSE_KW exprs
| IF_KW exprs THEN_KW exprs
| CHOOSE_KW exprs option+ default_option?
| USE parameters?
| USE parameters
| META_PROMPT exprs
| COMMENT_KW exprs
| VAR_NAME EQ_KW exprs
| VAR_NAME
| CALL positional_args parameters
;

option
Expand All @@ -40,8 +41,12 @@ default_option
: DEFAULT_KW exprs
;

positional_args
: (WITH_KW exprs)*
;

parameters
: (VAR_NAME EQ_KW exprs)+
: (VAR_NAME EQ_KW exprs)*
;

text: CHAR+ ;
Expand All @@ -56,10 +61,12 @@ fragment ESCAPED : ESCAPE ESCAPEE;
fragment ESCAPEE : (LB | ESCAPE);
fragment ESCAPE : '\\';
USE : ':use' WS+ [a-zA-Z0-9/_.-]+ WS*;
CALL : '@' [a-zA-Z_][a-zA-Z0-9_]* WS*;
fragment WS : ' '|'\n';
IF_KW : ':if' ;
CHOOSE_KW : ':choose' ;
OPTION_KW : ':option' ;
WITH_KW : ':with' ;
DEFAULT_KW : ':default' ;
IS_KW : ':is' ;
THEN_KW : ':then' ;
Expand Down
21 changes: 18 additions & 3 deletions python/src/parse_metaprompt.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ def visitParameters(self, ctx: MetaPromptParser.ParametersContext):
parameters[name.getText()[1:]] = self.visit(exprs)
return parameters

def visitPositional_args(
self, ctx: MetaPromptParser.Positional_argsContext
):
return [self.visit(expr) for expr in ctx.exprs()]

def visitOption(self, ctx: MetaPromptParser.OptionContext):
exprs = ctx.exprs()
option = self.visit(exprs[0])
Expand Down Expand Up @@ -155,15 +160,25 @@ def visitMeta_body(self, ctx: MetaPromptParser.Meta_bodyContext):

elif ctx.USE() is not None:
module_name = ctx.USE().getText().removeprefix(":use").strip()
parameters = {}
if ctx.parameters() is not None:
parameters = self.visit(ctx.parameters())
parameters = self.visit(ctx.parameters())
return {
"type": "use",
"module_name": module_name,
"parameters": parameters,
}

elif ctx.CALL() is not None:
function_name = ctx.CALL().getText().removeprefix("@").strip()
parameters = self.visit(ctx.parameters())
positional_args = self.visit(ctx.positional_args())

return {
"type": "call",
"name": function_name,
"named_args": parameters,
"positional_args": positional_args,
}

elif ctx.VAR_NAME() is not None:
# slice the ":"
var_name = ctx.VAR_NAME().getText()[1:]
Expand Down
7 changes: 6 additions & 1 deletion python/src/parser/MetaPrompt.interp
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ null
'#'
null
null
null
':if'
':choose'
':option'
':with'
':default'
':is'
':then'
Expand All @@ -25,9 +27,11 @@ META_PROMPT
COMMENT_KW
CHAR
USE
CALL
IF_KW
CHOOSE_KW
OPTION_KW
WITH_KW
DEFAULT_KW
IS_KW
THEN_KW
Expand All @@ -42,9 +46,10 @@ expr1
meta_body
option
default_option
positional_args
parameters
text


atn:
[4, 1, 15, 105, 2, 0, 7, 0, 2, 1, 7, 1, 2, 2, 7, 2, 2, 3, 7, 3, 2, 4, 7, 4, 2, 5, 7, 5, 2, 6, 7, 6, 2, 7, 7, 7, 2, 8, 7, 8, 1, 0, 1, 0, 1, 0, 1, 1, 5, 1, 23, 8, 1, 10, 1, 12, 1, 26, 9, 1, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 3, 2, 43, 8, 2, 1, 3, 1, 3, 3, 3, 47, 8, 3, 1, 4, 1, 4, 1, 4, 1, 4, 1, 4, 1, 4, 1, 4, 1, 4, 1, 4, 1, 4, 1, 4, 1, 4, 1, 4, 1, 4, 1, 4, 4, 4, 64, 8, 4, 11,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0, 0, 42, 36, 1, 0, 0, 0, 42, 37, 1, 0, 0, 0, 42, 38, 1, 0, 0, 0, 42, 39, 1, 0, 0, 0, 42, 40, 1, 0, 0, 0, 42, 41, 1, 0, 0, 0, 43, 5, 1, 0, 0, 0, 44, 47, 3, 8, 4, 0, 45, 47, 3, 2, 1, 0, 46, 44, 1, 0, 0, 0, 46, 45, 1, 0, 0, 0, 47, 7, 1, 0, 0, 0, 48, 49, 5, 8, 0, 0, 49, 50, 3, 2, 1, 0, 50, 51, 5, 13, 0, 0, 51, 52, 3, 2, 1, 0, 52, 53, 5, 14, 0, 0, 53, 54, 3, 2, 1, 0, 54, 83, 1, 0, 0, 0, 55, 56, 5, 8, 0, 0, 56, 57, 3, 2, 1, 0, 57, 58, 5, 13, 0, 0, 58, 59, 3, 2, 1, 0, 59, 83, 1, 0, 0, 0, 60, 61, 5, 9, 0, 0, 61, 63, 3, 2, 1, 0, 62, 64, 3, 10, 5, 0, 63, 62, 1, 0, 0, 0, 64, 65, 1, 0, 0, 0, 65, 63, 1, 0, 0, 0, 65, 66, 1, 0, 0, 0, 66, 68, 1, 0, 0, 0, 67, 69, 3, 12, 6, 0, 68, 67, 1, 0, 0, 0, 68, 69, 1, 0, 0, 0, 69, 83, 1, 0, 0, 0, 70, 72, 5, 7, 0, 0, 71, 73, 3, 14, 7, 0, 72, 71, 1, 0, 0, 0, 72, 73, 1, 0, 0, 0, 73, 83, 1, 0, 0, 0, 74, 75, 5, 4, 0, 0, 75, 83, 3, 2, 1, 0, 76, 77, 5, 5, 0, 0, 77, 83, 3, 2, 1, 0, 78, 79, 5, 15, 0, 0, 79, 80, 5, 3, 0, 0, 80, 83, 3, 2, 1, 0, 81, 83, 5, 15, 0, 0, 82, 48, 1, 0, 0, 0, 82, 55, 1, 0, 0, 0, 82, 60, 1, 0, 0, 0, 82, 70, 1, 0, 0, 0, 82, 74, 1, 0, 0, 0, 82, 76, 1, 0, 0, 0, 82, 78, 1, 0, 0, 0, 82, 81, 1, 0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, 5, 1, 5, 1, 5, 1, 5, 1, 6, 1, 6, 1, 6, 1, 7, 1, 7, 5, 7, 99, 8, 7, 10, 7, 12, 7, 102, 9, 7, 1, 8, 1, 8, 1, 8, 5, 8, 107, 8, 8, 10, 8, 12, 8, 110, 9, 8, 1, 9, 4, 9, 113, 8, 9, 11, 9, 12, 9, 114, 1, 9, 1, 26, 0, 10, 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 0, 0, 132, 0, 20, 1, 0, 0, 0, 2, 26, 1, 0, 0, 0, 4, 44, 1, 0, 0, 0, 6, 48, 1, 0, 0, 0, 8, 86, 1, 0, 0, 0, 10, 88, 1, 0, 0, 0, 12, 93, 1, 0, 0, 0, 14, 100, 1, 0, 0, 0, 16, 108, 1, 0, 0, 0, 18, 112, 1, 0, 0, 0, 20, 21, 3, 2, 1, 0, 21, 22, 5, 0, 0, 1, 22, 1, 1, 0, 0, 0, 23, 25, 3, 4, 2, 0, 24, 23, 1, 0, 0, 0, 25, 28, 1, 0, 0, 0, 26, 27, 1, 0, 0, 0, 26, 24, 1, 0, 0, 0, 27, 3, 1, 0, 0, 0, 28, 26, 1, 0, 0, 0, 29, 30, 5, 1, 0, 0, 30, 31, 3, 6, 3, 0, 31, 32, 5, 2, 0, 0, 32, 45, 1, 0, 0, 0, 33, 45, 3, 18, 9, 0, 34, 45, 5, 2, 0, 0, 35, 45, 5, 1, 0, 0, 36, 45, 5, 5, 0, 0, 37, 45, 5, 4, 0, 0, 38, 45, 5, 3, 0, 0, 39, 45, 5, 17, 0, 0, 40, 45, 5, 10, 0, 0, 41, 45, 5, 11, 0, 0, 42, 45, 5, 13, 0, 0, 43, 45, 5, 14, 0, 0, 44, 29, 1, 0, 0, 0, 44, 33, 1, 0, 0, 0, 44, 34, 1, 0, 0, 0, 44, 35, 1, 0, 0, 0, 44, 36, 1, 0, 0, 0, 44, 37, 1, 0, 0, 0, 44, 38, 1, 0, 0, 0, 44, 39, 1, 0, 0, 0, 44, 40, 1, 0, 0, 0, 44, 41, 1, 0, 0, 0, 44, 42, 1, 0, 0, 0, 44, 43, 1, 0, 0, 0, 45, 5, 1, 0, 0, 0, 46, 49, 3, 8, 4, 0, 47, 49, 3, 2, 1, 0, 48, 46, 1, 0, 0, 0, 48, 47, 1, 0, 0, 0, 49, 7, 1, 0, 0, 0, 50, 51, 5, 9, 0, 0, 51, 52, 3, 2, 1, 0, 52, 53, 5, 15, 0, 0, 53, 54, 3, 2, 1, 0, 54, 55, 5, 16, 0, 0, 55, 56, 3, 2, 1, 0, 56, 87, 1, 0, 0, 0, 57, 58, 5, 9, 0, 0, 58, 59, 3, 2, 1, 0, 59, 60, 5, 15, 0, 0, 60, 61, 3, 2, 1, 0, 61, 87, 1, 0, 0, 0, 62, 63, 5, 10, 0, 0, 63, 65, 3, 2, 1, 0, 64, 66, 3, 10, 5, 0, 65, 64, 1, 0, 0, 0, 66, 67, 1, 0, 0, 0, 67, 65, 1, 0, 0, 0, 67, 68, 1, 0, 0, 0, 68, 70, 1, 0, 0, 0, 69, 71, 3, 12, 6, 0, 70, 69, 1, 0, 0, 0, 70, 71, 1, 0, 0, 0, 71, 87, 1, 0, 0, 0, 72, 73, 5, 7, 0, 0, 73, 87, 3, 16, 8, 0, 74, 75, 5, 4, 0, 0, 75, 87, 3, 2, 1, 0, 76, 77, 5, 5, 0, 0, 77, 87, 3, 2, 1, 0, 78, 79, 5, 17, 0, 0, 79, 80, 5, 3, 0, 0, 80, 87, 3, 2, 1, 0, 81, 87, 5, 17, 0, 0, 82, 83, 5, 8, 0, 0, 83, 84, 3, 14, 7, 0, 84, 85, 3, 16, 8, 0, 85, 87, 1, 0, 0, 0, 86, 50, 1, 0, 0, 0, 86, 57, 1, 0, 0, 0, 86, 62, 1, 0, 0, 0, 86, 72, 1, 0, 0, 0, 86, 74, 1, 0, 0, 0, 86, 76, 1, 0, 0, 0, 86, 78, 1, 0, 0, 0, 86, 81, 1, 0, 0, 0, 86, 82, 1, 0, 0, 0, 87, 9, 1, 0, 0, 0, 88, 89, 5, 11, 0, 0, 89, 90, 3, 2, 1, 0, 90, 91, 5, 14, 0, 0, 91, 92, 3, 2, 1, 0, 92, 11, 1, 0, 0, 0, 93, 94, 5, 13, 0, 0, 94, 95, 3, 2, 1, 0, 95, 13, 1, 0, 0, 0, 96, 97, 5, 12, 0, 0, 97, 99, 3, 2, 1, 0, 98, 96, 1, 0, 0, 0, 99, 102, 1, 0, 0, 0, 100, 98, 1, 0, 0, 0, 100, 101, 1, 0, 0, 0, 101, 15, 1, 0, 0, 0, 102, 100, 1, 0, 0, 0, 103, 104, 5, 17, 0, 0, 104, 105, 5, 3, 0, 0, 105, 107, 3, 2, 1, 0, 106, 103, 1, 0, 0, 0, 107, 110, 1, 0, 0, 0, 108, 106, 1, 0, 0, 0, 108, 109, 1, 0, 0, 0, 109, 17, 1, 0, 0, 0, 110, 108, 1, 0, 0, 0, 111, 113, 5, 6, 0, 0, 112, 111, 1, 0, 0, 0, 113, 114, 1, 0, 0, 0, 114, 112, 1, 0, 0, 0, 114, 115, 1, 0, 0, 0, 115, 19, 1, 0, 0, 0, 9, 26, 44, 48, 67, 70, 86, 100, 108, 114]
33 changes: 18 additions & 15 deletions python/src/parser/MetaPrompt.tokens
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,24 @@ META_PROMPT=4
COMMENT_KW=5
CHAR=6
USE=7
IF_KW=8
CHOOSE_KW=9
OPTION_KW=10
DEFAULT_KW=11
IS_KW=12
THEN_KW=13
ELSE_KW=14
VAR_NAME=15
CALL=8
IF_KW=9
CHOOSE_KW=10
OPTION_KW=11
WITH_KW=12
DEFAULT_KW=13
IS_KW=14
THEN_KW=15
ELSE_KW=16
VAR_NAME=17
'['=1
']'=2
'#'=5
':if'=8
':choose'=9
':option'=10
':default'=11
':is'=12
':then'=13
':else'=14
':if'=9
':choose'=10
':option'=11
':with'=12
':default'=13
':is'=14
':then'=15
':else'=16
Loading

0 comments on commit 04bec7b

Please sign in to comment.