Browse Source

grammar: indentation after escaped newline is ignored

AST, Parser: read parameters for function defenitions
master
Pixdigit 2 years ago
parent
commit
ca07554cbf
  1. 4
      ScriptoidLexer.g4
  2. 6
      ScriptoidParser.g4
  3. 5
      internal/ast/FunctionNode.go
  4. 17
      internal/ast/ParameterNode.go
  5. 18
      internal/treeVisitor.go
  6. 9
      test.stc

4
ScriptoidLexer.g4

@ -25,11 +25,11 @@ FLOAT_LIT: [1-9][0-9]*'.'[0-9]+;
STRING: '"' -> more, pushMode(STR);
NEWLINE: [\r\n];
ESCAPED_NEWLINE: '\\'[\r\n];
ESCAPED_NEWLINE: '\\'[\r\n]([\t ]*)? -> channel(HIDDEN);
INDENT: '\t';
SPACE: ' ';
WS: (SPACE);
WS: SPACE;
COMMENT: '/*' .*? '*/' -> channel(HIDDEN);
LINE_COMMENT: '//' ~[\r\n]* -> channel(HIDDEN);

6
ScriptoidParser.g4

@ -20,9 +20,9 @@ declaration:
variable: VAR SPACE IDENTIFIER SPACE ASSIGN SPACE literal;
function: FUNC SPACE IDENTIFIER SPACE? COLON SPACE (params SPACE)? block;
function_literal: '(' params? ')' SPACE FUNC_LIT SPACE block;
params: IDENTIFIER (COMMA SPACE IDENTIFIER)*;
function: FUNC SPACE IDENTIFIER SPACE? COLON SPACE (parameters SPACE)? block;
function_literal: '(' parameters? ')' SPACE FUNC_LIT SPACE block;
parameters: IDENTIFIER (COMMA SPACE IDENTIFIER)*;
args: (IDENTIFIER | literal) (COMMA SPACE (IDENTIFIER | literal))*;
call: (IDENTIFIER | function | function_literal) '(' args? ')';

5
internal/ast/FunctionNode.go

@ -3,16 +3,19 @@ package ast
type FunctionNode struct {
BaseNode
*IdentifierNode
Parameter *ParameterNode
Scope *SemanticNode
}
func NewFunctionNode(identifier *IdentifierNode, scope *SemanticNode) *FunctionNode {
func NewFunctionNode(identifier *IdentifierNode, params *ParameterNode, scope *SemanticNode) *FunctionNode {
node := &FunctionNode{
*NewBaseNode(),
identifier,
params,
scope,
}
node.AddChild(identifier)
node.AddChild(params)
node.AddChild(scope)
return node
}

17
internal/ast/ParameterNode.go

@ -0,0 +1,17 @@
package ast
type ParameterNode struct {
BaseNode
Parameters []*IdentifierNode
}
func NewParameterNode(params []*IdentifierNode) *ParameterNode {
node := &ParameterNode{
*NewBaseNode(),
params,
}
for _, param := range params {
node.AddChild(param)
}
return node
}

18
internal/treeVisitor.go

@ -111,14 +111,24 @@ func (s *ScriptoidTreeVisitor) VisitFunction(ctx *parser.FunctionContext) interf
s.printf("Visiting Function \"%v\"", ctx.IDENTIFIER().GetSymbol().GetText())
func_name := ast.NewIdentifierNode(ctx.IDENTIFIER().GetText())
parameters := s.visitSubTree("parameter", ctx.Params())
parameters := s.visitSubTree("parameter", ctx.Parameters()).(*ast.ParameterNode)
scope := s.visitSubTree("block", ctx.Block()).(*ast.SemanticNode)
parameters = parameters
return ast.NewFunctionNode(func_name, scope)
return ast.NewFunctionNode(func_name, parameters, scope)
}
func (s *ScriptoidTreeVisitor) VisitBlock(ctx *parser.BlockContext) interface{} {
return s.visitSubTree("scope", ctx.Scope())
}
func (s *ScriptoidTreeVisitor) VisitParameters(ctx *parser.ParametersContext) interface{} {
s.printf("Found %v Parameters", len(ctx.AllIDENTIFIER()))
parameters := make([]*ast.IdentifierNode, len(ctx.AllIDENTIFIER()))
for i, ident := range ctx.AllIDENTIFIER() {
parameters[i] = ast.NewIdentifierNode(ident.GetText())
}
return ast.NewParameterNode(parameters)
}

9
test.stc

@ -1,6 +1,7 @@
var test = "test"
var test = \
"test";
var New_test_variable = 7; var b = 2
//var test = New_test_variable + b
@ -12,6 +13,10 @@ func add: a, b {
var res = add(test, 6)
var test = (c, d) => {
var test2 = (c, d) => {
}
var test3 = (t) => {
}(test)

Loading…
Cancel
Save