private static List <Spanned <FunctionParameter> > ParseFunctionParameters( Context context, ParserState state) { var parameters = new List <Spanned <FunctionParameter> >(); state.ConsumeOrError(TokenKind.LeftParenthesis); if (state.Consume(TokenKind.RightParenthesis)) { return(parameters); } do { var identifier = state.ConsumeOrError(TokenKind.Identifier); state.ConsumeOrError(TokenKind.Colon); var typeSignature = TypeSignatureParser.ParseTypeSignature(state); var span = SourceSpan.Merge(identifier.Span, typeSignature.Span); parameters.Add( new Spanned <FunctionParameter>( new FunctionParameter( state.NodeIdGenerator.GetNextId(), identifier, typeSignature), span)); } while (!state.IsAtEnd() && state.Consume(TokenKind.Comma)); state.ConsumeOrError(TokenKind.RightParenthesis); return(parameters); }
private static IStatement ParseFunctionDefinition(Context context, ParserState state) { var startSpan = state.Previous().Span; var identifier = state.ConsumeOrError(TokenKind.Identifier); var parameters = ParseFunctionParameters(context, state); var returnTypeSignature = state.Consume(TokenKind.Colon) ? TypeSignatureParser.ParseTypeSignature(state) : null; var body = ParseBlock(context, state); var endSpan = state.Previous().Span; var span = SourceSpan.Merge(startSpan, endSpan); return(new FunctionDefinition( state.NodeIdGenerator.GetNextId(), span, identifier, parameters, body, returnTypeSignature)); }
private static IStatement ParseVariableDefinition(Context context, ParserState state) { var startSpan = state.Previous().Span; var identifier = state.ConsumeOrError(TokenKind.Identifier); var typeSignature = state.Consume(TokenKind.Colon) ? TypeSignatureParser.ParseTypeSignature(state) : null; SourceSpan endSpan; SourceSpan span; if (state.Consume(TokenKind.Equal)) { var initializer = ExpressionParser.ParseExpression(state); endSpan = state.Previous().Span; span = SourceSpan.Merge(startSpan, endSpan); return(new VariableDefinition( state.NodeIdGenerator.GetNextId(), span, identifier, typeSignature, initializer)); } if (typeSignature == null) { throw new ParseException(identifier.Span, "type signature needed"); } endSpan = state.Previous().Span; span = SourceSpan.Merge(startSpan, endSpan); return(new VariableDefinition( state.NodeIdGenerator.GetNextId(), span, identifier, typeSignature, null)); }