Exemplo n.º 1
0
func (fs LambdaStatement) Parse(block string, lineNum int, nextBlockScanner *parser.ScanPeeker, factory *StatementFactory) (Statement, parser.SyntaxError) {
	lines := parser.Lines(block)
	factory = fetchNewFactory(factory)
	typeDef, err, rest := expressionParsing.ParseTypeDef(lines[0])
	if err != nil {
		return nil, parser.NewSyntaxError(err.Error(), lineNum, 0)
	}

	var ftd expressionParsing.FuncTypeDefinition
	var ok bool
	if ftd, ok = typeDef.(expressionParsing.FuncTypeDefinition); !ok {
		return nil, nil
	}

	var codeBlock []string
	if len(strings.TrimSpace(rest)) > 0 {
		var first string
		if len(lines) > 1 {
			return nil, parser.NewSyntaxError("Inline lambdas can only be one line", lineNum, 0)
		} else if first, rest = parser.Tokenize(rest); first != "->" {
			return nil, parser.NewSyntaxError("Misplaced tokens: "+rest, lineNum, 0)
		}
		codeBlock = []string{rest}
	} else {
		codeBlock = parser.RemoveTabs(lines[1:])
	}

	innerStatements, synErr := fetchInnerStatements(codeBlock, factory, lineNum+1)
	if synErr != nil {
		return nil, synErr
	}

	synErr = verifyInnerStatements(innerStatements, lineNum)
	if synErr != nil {
		return nil, synErr
	}

	return newLambdaStatement(lineNum, ftd, innerStatements, fs.packageLevel, fs.name), nil
}
Exemplo n.º 2
0
func (ds LetStatement) Parse(block string, lineNum int, nextBlockScanner *parser.ScanPeeker, factory *StatementFactory) (Statement, parser.SyntaxError) {
	lines := parser.Lines(block)

	ok, varName, restOfLine := splitEquals(lines[0])

	if ok {
		if ds.packageLevel {
			factory = adjustFactory(varName, factory)
		}
		combinedLine := parser.FromLines(append([]string{restOfLine}, lines[1:]...))
		peeker := parser.NewScanPeekerStr(combinedLine, lineNum)
		st, err := factory.Read(peeker)
		if err != nil {
			return nil, err
		}
		return newLetStatement(varName, combinedLine, lineNum, st), nil
	}

	if ds.packageLevel {
		return nil, parser.NewSyntaxError(fmt.Sprintf("Unknown statement: %s", lines[0]), lineNum, 0)
	} else {
		return nil, nil
	}
}