import * as Immutable from 'immutable'; import { apply, co_error, co_get_state, co_set_state, co_unit, Coroutine, inl, inr, Option, Prod, Sum, Unit } from 'ts-bccc'; import * as CCC from 'ts-bccc'; import { co_catch, co_lookup, co_stateless, co_repeat } from '../ccc_aux'; import { join_source_ranges, mk_range, SourceRange, zero_range } from '../source_range'; import { BinOpKind, Token, UnaryOpKind } from './lexer'; import { and_op, arrow_op, class_keyword, comma_sign, div_op, dot_sign, else_keyword, eof, eq_op, equal_sign, for_keyword, geq_op, gt_op, identifier, identifier_token, if_keyword, ignore_whitespace, left_bracket, left_curly_bracket, left_square_bracket, leq_op, lt_op, merge_errors, minus_op, mk_and, mk_array_cons_call, mk_array_decl, mk_arrow, mk_assign, mk_bracket, mk_call, mk_class_declaration, mk_constructor_call, mk_constructor_declaration, mk_dbg, mk_decl, mk_decl_and_init, mk_div, mk_eq, mk_field_ref, mk_for, mk_function_declaration, mk_generic_type_inst, mk_geq, mk_get_array_value_at, mk_gt, mk_if_then, mk_if_then_else, mk_leq, mk_lt, mk_minus, mk_mod, mk_neq, mk_noop, mk_or, mk_override, mk_pair, mk_plus, mk_private, mk_protected, mk_public, mk_record_type_decl, mk_return, mk_semicolon, mk_static, mk_tc_dbg, mk_times, mk_tuple_type_decl, mk_unit, mk_virtual, mk_while, mk_xor, mod_op, neq_op, new_keyword, or_op, override_modifier, plus_op, private_modifier, protected_modifier, public_modifier, return_sign, right_bracket, right_curly_bracket, right_square_bracket, semicolon_sign, static_modifier, term, times_op, virtual_modifier, while_keyword, xor_op, negative_number, parser_or, mk_not, not_op, mk_array_cons_call_and_init, question_mark_keyword, colon_keyword, mk_ternary_if, mk_ternary_then_else, mk_as, as_op, base, abstract_modifier, mk_abstract, mk_interface, interface_modifier, string, filesystem_keyword, file_keyword } from './primitives'; import { Modifier, GenericParameter } from './types'; let priority_operators_table = Immutable.Map() .set("()", {priority:12, associativity:"left"}) .set("[]", {priority:12, associativity:"left"}) .set(".", {priority:12, associativity:"left"}) .set("*", {priority:10, associativity:"left"}) .set("/", {priority:10, associativity:"left"}) .set("%", {priority:10, associativity:"right"}) .set("+", {priority:7, associativity:"left"}) .set("-", {priority:7, associativity:"left"}) .set(">", {priority:6, associativity:"right"}) .set("<", {priority:6, associativity:"right"}) .set("<=", {priority:6, associativity:"right"}) .set(">=", {priority:6, associativity:"right"}) .set("==", {priority:5, associativity:"right"}) .set("!=", {priority:5, associativity:"right"}) .set("not", {priority:4, associativity:"right"}) .set("xor", {priority:4, associativity:"right"}) .set("&&", {priority:4, associativity:"right"}) .set("||", {priority:4, associativity:"right"}) .set("as", {priority:4, associativity:"right"}) .set(":", {priority:3, associativity:"right"}) .set("?", {priority:2, associativity:"left"}) .set("=>", {priority:1, associativity:"right"}) .set(",", {priority:0, associativity:"right"}) export type ModifierAST = { kind:"abstract" } | { kind:"private" } | { kind:"public" } | { kind:"static" } | { kind:"protected" } | { kind:"virtual" } | { kind:"override" } | { kind:"interface" } export interface DebuggerAST { kind: "debugger" } export interface TCDebuggerAST { kind: "typechecker_debugger" } export interface StringAST { kind: "string", value:string } export interface BoolAST { kind: "bool", value: boolean } export interface IntAST { kind: "int", value: number } export interface FloatAST { kind: "float", value: number } export interface DoubleAST { kind: "double", value: number } export interface IdAST { kind: "id", value: string, optional_params: ParserRes[] } export interface ForAST { kind: "for", i:ParserRes, c:ParserRes, s:ParserRes, b:ParserRes } export interface WhileAST { kind: "while", c:ParserRes, b:ParserRes } export interface IfAST { kind: "if", c:ParserRes, t:ParserRes, e:Option } export interface DeclAST { kind: "decl", l:ParserRes, r:ParserRes } export interface DeclAndInitAST { kind: "decl and init", l:ParserRes, r:ParserRes, v:ParserRes } export interface AssignAST { kind: "=", l:ParserRes, r:ParserRes } export interface FieldRefAST { kind: ".", l:ParserRes, r:ParserRes } export interface SemicolonAST { kind: ";", l:ParserRes, r:ParserRes } export interface ReturnAST { kind: "return", value:ParserRes } export interface NoopAST { kind: "noop" } export interface ArgsAST { kind: "args", value:Immutable.List } export interface BracketAST { kind:"bracket", e:ParserRes } export interface UnitAST { kind: "unit" } export interface FieldAST { decl:DeclAST | DeclAndInitAST, modifiers:Immutable.List<{ range:SourceRange, ast:ModifierAST }> } export interface MethodAST { decl:FunctionDeclarationAST, modifiers:Immutable.List<{ range:SourceRange, ast:ModifierAST }> } export interface ConstructorAST { decl:ConstructorDeclarationAST, modifiers:Immutable.List<{ range:SourceRange, ast:ModifierAST }> } export interface ClassAST { kind: "class", C_name:string, generic_parameters:{ name:ParserRes, variant:"co"|"contra"|"inv" }[], extends_or_implements:{C_name:string, generic_parameters:GenericParameter[], ast:ParserRes}[], fields:Immutable.List, methods:Immutable.List, generic_methods:Immutable.List, constructors:Immutable.List, modifiers:Immutable.List } export interface ConstructorDeclarationAST { kind:"cons_decl", range:SourceRange, name:string, arg_decls:Immutable.List, params_base_call:Option, body:ParserRes } export interface BinOpAST { kind: BinOpKind, l:ParserRes, r:ParserRes } export interface UnaryOpAST { kind: UnaryOpKind, e:ParserRes } export interface FunctionDeclarationAST { kind:"func_decl", params_base_call:ParserRes[], range:SourceRange, name:string, return_type:ParserRes, arg_decls:Immutable.List, body:ParserRes, generic_parameters:ParserRes[] } export interface FunctionCallAST { kind:"func_call", name:ParserRes, actuals:Array } export interface ConstructorCallAST { kind:"cons_call", name:string, type_args:Array, actuals:Array } export interface ArrayConstructorCallAST { kind:"array_cons_call", type:ParserRes, actual:ParserRes } export interface ArrayConstructorCallAndInitAST { kind:"array_cons_call_and_init", type:ParserRes, actuals:ParserRes[] } export interface GetArrayValueAtAST {kind:"get_array_value_at", array:ParserRes, index:ParserRes } export interface TernaryIfAST {kind:"ternary_if", condition:ParserRes, then_else:ParserRes } export interface TernaryThenElseAST {kind:"ternary_then_else", _then:ParserRes, _else:ParserRes } export interface EmptySurface { kind: "empty surface", w:ParserRes, h:ParserRes, color:ParserRes } export interface Sprite { kind: "sprite", cx:ParserRes, cy:ParserRes, w:ParserRes, h:ParserRes, sprite:ParserRes, rotation:ParserRes } export interface Circle { kind: "circle", cx:ParserRes, cy:ParserRes, r:ParserRes, color:ParserRes } export interface Square { kind: "square", cx:ParserRes, cy:ParserRes, s:ParserRes, color:ParserRes, rotation:ParserRes } export interface Ellipse { kind: "ellipse", cx:ParserRes, cy:ParserRes, w:ParserRes, h:ParserRes, color:ParserRes, rotation:ParserRes } export interface Rectangle { kind: "rectangle", cx:ParserRes, cy:ParserRes, w:ParserRes, h:ParserRes, color:ParserRes, rotation:ParserRes } export interface Line { kind:"line", x1:ParserRes, y1:ParserRes, x2:ParserRes, y2:ParserRes, width:ParserRes, color:ParserRes, rotation:ParserRes } export interface Polygon { kind:"polygon", points:ParserRes, color:ParserRes, rotation:ParserRes } export interface Text { kind:"text", t:ParserRes, x:ParserRes, y:ParserRes, size:ParserRes, color:ParserRes, rotation:ParserRes } export interface OtherSurface { kind: "other surface", s:ParserRes, dx:ParserRes, dy:ParserRes, sx:ParserRes, sy:ParserRes, rotation:ParserRes } export type RenderSurfaceAST = EmptySurface | Circle | Square | Ellipse | Rectangle | Line | Polygon | Text | Sprite | OtherSurface export interface GenericTypeDeclAST { kind:"generic type inst", f:ParserRes, args:Array } export interface ArrayTypeDeclAST { kind:"array decl", t:ParserRes } export interface TupleTypeDeclAST { kind:"tuple type decl", args:Array } export interface RecordTypeDeclAST { kind:"record type decl", args:Array } export interface FileSystemAST { kind: 'filesystem', nodes: Immutable.List } export interface DirectoryAST { kind: 'filesystem.directory', path: ParserRes, nodes: Immutable.List } export interface FileAST { kind: 'filesystem.file', path: ParserRes, attributes: Immutable.List } export interface KeyValuePairAST { kind: 'filesystem.keyvalue', key: ParserRes, value: ParserRes } export interface FSAndProgramAST { kind: 'filesystem+program', filesystem: ParserRes, program: ParserRes } export const mk_filesystem_ast = (nodes: ParserRes[]): AST => ({ kind: 'filesystem', nodes: Immutable.List(nodes) }) export const mk_key_value_ast = (key: ParserRes, value: ParserRes): AST => ({ kind: 'filesystem.keyvalue', key: key, value: value }) export const mk_file_ast = (path: ParserRes, contents: ParserRes[]): AST => ({ kind: 'filesystem.file', path: path, attributes: Immutable.List(contents) }) export const mk_filesys_and_program_ast = (fs: ParserRes, prg: ParserRes): AST => ({ kind: 'filesystem+program', filesystem: fs, program: prg }) export const key_value: Coroutine = string.then(key => colon_keyword.then(_ => string.then(value => co_unit({ range: join_source_ranges(key.range, value.range), ast: mk_key_value_ast(key, value) }) ))) export const file: Coroutine = file_keyword.then(id => string.then(path => left_curly_bracket.then(_ => co_repeat(key_value).then(kvs => right_curly_bracket.then(rb => co_unit({ range: join_source_ranges(id, rb), ast: mk_file_ast(path, kvs) }) ))))) export const filesystem_prs: Coroutine = filesystem_keyword.then(k => left_curly_bracket.then(_ => co_repeat(file).then(children => right_curly_bracket.then(rb => co_unit({ range: join_source_ranges(k, rb), ast: mk_filesystem_ast(children) }) )))) export type AST = UnitAST | StringAST | IntAST | FloatAST | DoubleAST | BoolAST | IdAST | FieldRefAST | GenericTypeDeclAST | TupleTypeDeclAST | RecordTypeDeclAST | AssignAST | DeclAST | DeclAndInitAST | IfAST | ForAST | WhileAST | SemicolonAST | ReturnAST | ArgsAST | BinOpAST | UnaryOpAST | FunctionDeclarationAST | FunctionCallAST | ClassAST | ConstructorCallAST | ArrayConstructorCallAST | DebuggerAST | TCDebuggerAST | NoopAST | RenderSurfaceAST | ArrayTypeDeclAST | ModifierAST | GetArrayValueAtAST | BracketAST | ArrayConstructorCallAndInitAST | TernaryIfAST | TernaryThenElseAST | FileSystemAST | FileAST | KeyValuePairAST | FSAndProgramAST export interface ParserRes { range:SourceRange, ast:AST } export interface ParserError { priority:number, message:string, range:SourceRange } export interface ParserState { tokens:Immutable.List, branch_priority:number } export type Parser = Coroutine export let mk_parser_state = (tokens:Immutable.List) => ({ tokens:tokens, branch_priority:0 }) let no_match : Coroutine = co_get_state().then(s => co_set_state({...s, branch_priority:0})) let partial_match : Coroutine = co_get_state().then(s => co_set_state({...s, branch_priority:50})) let full_match : Coroutine = co_get_state().then(s => co_set_state({...s, branch_priority:100})) let dbg: Parser = ignore_whitespace(co_get_state().then(s => { if (s.tokens.isEmpty()) return co_error({ range:mk_range(-1,0,0,0), priority:s.branch_priority, message:"found empty state, expected identifier" }) let i = s.tokens.first() if (i.kind == "debugger") { let res = mk_dbg(i.range) return co_set_state({...s, tokens: s.tokens.rest().toList() }).then(_ => co_unit(res)) } else return co_error({ range:i.range, priority:s.branch_priority, message:`expected debugger but found ${i.kind}` }) })) let tc_dbg: Parser = ignore_whitespace(co_get_state().then(s => { if (s.tokens.isEmpty()) return co_error({ range:mk_range(-1,0,0,0), priority:s.branch_priority, message:"found empty state, expected identifier" }) let i = s.tokens.first() if (i.kind == "typechecker_debugger") { let res = mk_tc_dbg(i.range) return co_set_state({...s, tokens: s.tokens.rest().toList() }).then(_ => co_unit(res)) } else return co_error({ range:i.range, priority:s.branch_priority, message:`expected typecheker debugger but found ${i.kind}` }) })) let index_of : Coroutine = left_square_bracket.then(ls => expr().then(actual => right_square_bracket.then(rs => co_unit({val:actual, range:join_source_ranges(ls,rs)}) ))) export let par : Coroutine = no_match.then(_ => left_bracket.then(lb => partial_match.then(_ => actuals().then((actuals:Array) => right_bracket.then(rb => full_match.then(_ => co_unit({val:actuals, range:join_source_ranges(lb,rb)}))))))) let empty_table = { symbols: Immutable.Stack(), callables: Immutable.Stack(), ops: Immutable.Stack>() } let reduce_table = (table: { symbols: Immutable.Stack, callables: Immutable.Stack, ops: Immutable.Stack> }): ParserRes => { if (table.symbols.count() == 0 && table.ops.count() == 0) return { ast: { kind: "unit" }, range: mk_range(-1, -1, -1, -1) } if (table.symbols.count() == 1 && table.ops.count() == 0) return table.symbols.peek() let res = reduce_table_2(table.symbols, table.ops, table.callables, true) return res.new_top } let is_callable = (e: ParserRes): boolean => { let e_k = e.ast.kind // console.log("") // console.log("is callable", e_k) // console.log("") return e_k == "." || e_k == "func_call" || e_k == "get_array_value_at" || e_k == "bracket" || e_k == "id" } let reduce_table_2 = (symbols: Immutable.Stack, ops: Immutable.Stack>, callables: Immutable.Stack, reduce_to_end: boolean): { new_top: ParserRes, symbols: Immutable.Stack, ops: Immutable.Stack>, callables: Immutable.Stack } => { if (reduce_to_end && symbols.count() == 1 && ops.count() == 0) { return { new_top: symbols.peek(), callables: callables.pop().push(is_callable(symbols.peek())), symbols: symbols, ops: ops } } let op = ops.peek() // console.log("ops_count", ops.count(), JSON.stringify(ops)) // console.log("symbols_count", symbols.count(), JSON.stringify(symbols)) // console.log("callables_count", callables.count(), JSON.stringify(callables)) // console.log("op", callables.count(), JSON.stringify(callables)) if (op.snd.kind == "binary") { let snd = symbols.peek() let fst = symbols.pop().peek() symbols = symbols.pop().pop() let new_top = op.snd.f(fst, snd) callables = callables.pop().pop().pop() let is_new_top_callable = is_callable(new_top) callables = callables.push(is_new_top_callable) if (reduce_to_end) { return reduce_table_2(symbols.push(new_top), ops.pop(), callables, reduce_to_end) } return { new_top: new_top, symbols: symbols.push(new_top), callables: callables, ops: ops.pop() } } else { let fst = symbols.peek() symbols = symbols.pop() let is_fst_callable = fst == undefined ? false : callables.count() == 0 ? false : callables.peek() callables = callables.pop() let new_top = op.snd.f(fst == undefined ? "none" : fst, is_fst_callable) if (new_top.kind == "0-ary_push_back") { symbols = fst == undefined ? symbols : symbols.push(fst) symbols = symbols.push(new_top.value) callables = callables.pop().push(is_callable(new_top.value)) } else { callables = callables.pop().pop().push(is_callable(new_top.value)) symbols = symbols.push(new_top.value) } if (reduce_to_end && symbols.count() > 0) { return reduce_table_2(symbols, ops.pop(), callables, reduce_to_end) } return { new_top: new_top.value, symbols: symbols, callables: callables, ops: ops.pop() } } } let expr_after_op = (symbols: Immutable.Stack, callables: Immutable.Stack, ops: Immutable.Stack>, current_op: string, compose_current: unary_or_binary_op): Coroutine, callables: Immutable.Stack, ops: Immutable.Stack> }> => { if (ops.count() > 0 && ((ops.peek().snd.kind == "binary" && symbols.count() >= 2) || (ops.peek().snd.kind == "unary" && symbols.count() >= 1) )) { // console.log("A") let op_p = priority_operators_table.get(ops.peek().fst) let current_p = priority_operators_table.get(current_op) if (op_p.priority > current_p.priority || (op_p.priority == current_p.priority && op_p.associativity == "left")) { let res = reduce_table_2(symbols, ops, callables, false) return expr_after_op(res.symbols, res.callables, res.ops, current_op, compose_current) } } // console.log("B") return expr_AUX({ symbols: symbols, ops: ops.push({ fst: current_op, snd: compose_current }), callables: current_op == "()" ? callables : callables.push(false) }) } type SymTable = { symbols: Immutable.Stack, callables: Immutable.Stack, ops: Immutable.Stack> } let comma_to_array = (comma:ParserRes) : ParserRes[] => { if(comma.ast.kind == ","){ let left = comma.ast.l let right = comma_to_array(comma.ast.r) return [left].concat(right) } else { return [comma] } } type unary_or_binary_op = { kind: "binary", f: (l: ParserRes, r: ParserRes) => ParserRes } | { kind: "unary", f: (l: ParserRes | "none", is_callable: boolean) => { kind: "res" | "0-ary_push_back", value: ParserRes } } let mk_unary = (f: (l: ParserRes | "none", is_callable: boolean) => { kind: "res" | "0-ary_push_back", value: ParserRes }): unary_or_binary_op => ({ kind: "unary", f: f }) let mk_binary = (f: (l: ParserRes, r: ParserRes) => ParserRes): unary_or_binary_op => ({ kind: "binary", f: f }) let expr_AUX = ( table: { symbols: Immutable.Stack, callables: Immutable.Stack, ops: Immutable.Stack> }, try_par?:boolean): Coroutine => { let cases = (l: ParserRes | "none") => { let symbols = table.symbols let callables = table.callables if (l != "none") { symbols = table.symbols.push(l) callables = table.callables.push(is_callable(l)) } else { } // to improve // not_op.then(_ => // expr().then(e => // co_unit(mk_not(e)))) return parser_or(not_op.then(_ => expr_after_op(symbols, callables, table.ops, "not", mk_unary((l, is_callable) => ({ kind: "res", value: mk_not(l as any) })))), parser_or(index_of.then(res => expr_after_op(symbols, callables, table.ops, "[]", mk_unary((l, is_callable) => ({ kind: "res", value: mk_get_array_value_at(join_source_ranges((l as ParserRes).range, res.range), l as any, res.val) })))), parser_or(question_mark_keyword.then(_ => expr_after_op(symbols, callables, table.ops, "?", mk_binary((l, r) => mk_ternary_if(join_source_ranges(l.range, r.range) ,l, r)))), parser_or(colon_keyword.then(_ => expr_after_op(symbols, callables, table.ops, ":", mk_binary((l, r) => mk_ternary_then_else(join_source_ranges(l.range, r.range) ,l, r)))), parser_or(dot_sign.then(_ => expr_after_op(symbols, callables, table.ops, ".", mk_binary((l, r) => mk_field_ref(l, r)))), parser_or(par.then(res => { let actuals = res.val let range = res.range actuals = actuals.length == 1 && actuals[0].ast.kind == "unit" ? [] : actuals return expr_after_op(symbols, l!= "none" ? callables.push(is_callable(l)) : callables, table.ops, "()", mk_unary((_l, is_callable) => { return _l == "none" ? { kind: "0-ary_push_back", value: mk_bracket(actuals[0], range) } : !is_callable ? { kind: "0-ary_push_back", value: mk_bracket(actuals[0], range) } : { kind: "res", value: mk_call(_l, actuals.length == 1 && actuals[0].ast.kind == "," ? comma_to_array(actuals[0]) : actuals, join_source_ranges(_l.range, res.range)) } })) }), parser_or(comma.then(_ => expr_after_op(symbols, callables, table.ops, ",", mk_binary((l, r) => mk_pair(l, r)))), parser_or(arrow_op.then(_ => expr_after_op(symbols, callables, table.ops, "=>", mk_binary((l, r) => //console.log("mk_arrow-1", JSON.stringify(l)) || //console.log("mk_arrow-2", JSON.stringify(r)) || mk_arrow(l, r)))), parser_or(as_op.then(_ => expr_after_op(symbols, callables, table.ops, "as", mk_binary((l, r) => mk_as(l, r)))), parser_or(plus_op.then(_ => expr_after_op(symbols, callables, table.ops, "+", mk_binary((l, r) => mk_plus(l, r)))), parser_or(minus_op.then(_ => expr_after_op(symbols, callables, table.ops, "-", mk_binary((l, r) => mk_minus(l, r)))), parser_or(co_stateless(negative_number).then(_ => expr_after_op(symbols, callables, table.ops, "+", mk_binary((l, r) => mk_plus(l, r)))), parser_or(times_op.then(_ => expr_after_op(symbols, callables, table.ops, "*", mk_binary((l, r) => mk_times(l, r)))), parser_or(div_op.then(_ => expr_after_op(symbols, callables, table.ops, "/", mk_binary((l, r) => mk_div(l, r)))), parser_or(mod_op.then(_ => expr_after_op(symbols, callables, table.ops, "%", mk_binary((l, r) => mk_mod(l, r)))), parser_or(lt_op.then(_ => expr_after_op(symbols, callables, table.ops, "<", mk_binary((l, r) => mk_lt(l, r)))), parser_or(gt_op.then(_ => expr_after_op(symbols, callables, table.ops, ">", mk_binary((l, r) => mk_gt(l, r)))), parser_or(leq_op.then(_ => expr_after_op(symbols, callables, table.ops, "<=", mk_binary((l, r) => mk_leq(l, r)))), parser_or(geq_op.then(_ => expr_after_op(symbols, callables, table.ops, ">=", mk_binary((l, r) => mk_geq(l, r)))), parser_or(eq_op.then(_ => expr_after_op(symbols, callables, table.ops, "==", mk_binary((l, r) => mk_eq(l, r)))), parser_or(neq_op.then(_ => expr_after_op(symbols, callables, table.ops, "!=", mk_binary((l, r) => mk_neq(l, r)))), parser_or(and_op.then(_ => expr_after_op(symbols, callables, table.ops, "&&", mk_binary((l, r) => mk_and(l, r)))), parser_or(or_op.then(_ => expr_after_op(symbols, callables, table.ops, "||", mk_binary((l, r) => mk_or(l, r)))), parser_or(xor_op.then(_ => expr_after_op(symbols, callables, table.ops, "xor", mk_binary((l, r) => mk_xor(l, r)))), co_unit({ ...table, symbols: symbols, callables: callables }) )))))))))))))))))))))))) } // return parser_or(term().then(l => cases(l).then(res => console.log("RES1", res)||co_unit(res))), cases("none").then(res => console.log("RES2", res) || co_unit(res))) return parser_or(term(try_par?try_par:false).then(l => cases(l)), cases("none")) } let cons_call = () : Coroutine => new_keyword.then(new_range => identifier_token.then(class_name => parser_or>( lt_op.then(_ => partial_match.then(_ => type_args().then(args => gt_op.then(end_range => co_unit(args) )))),co_unit(Array())).then(type_params => left_bracket.then(_ => actuals().then((actuals:Array) => right_bracket.then(rb => { let args = actuals.length == 1 && actuals[0].ast.kind == "unit" ? [] : actuals.length == 1 && actuals[0].ast.kind == "," ? comma_to_array(actuals[0]) : actuals return co_unit(mk_constructor_call(join_source_ranges(new_range, rb), class_name.id, type_params, args))} )))))) let array_new = () : Coroutine => new_keyword.then(new_range => type_decl().then(array_type => left_square_bracket.then(_ => parser_or(expr(), term(false)) .then((actual:ParserRes) => right_square_bracket.then(rs => co_unit(mk_array_cons_call(join_source_ranges(new_range, rs), array_type, actual)) ))))) let array_new_and_init = () : Coroutine => new_keyword.then(new_range => type_decl(false).then(array_type => left_square_bracket.then(_ => right_square_bracket.then(_ => left_curly_bracket.then(_ => actuals().then(_actuals => right_curly_bracket.then(rs => { let actuals = _actuals[0].ast.kind == "," ? comma_to_array(_actuals[0]) : _actuals return co_unit(mk_array_cons_call_and_init(join_source_ranges(new_range, rs), array_type, actuals)) } ))))))) export let expr = () : Coroutine => { let res = expr_AUX(empty_table, true).then(e => co_unit(reduce_table(e))) return parser_or(array_new_and_init(), parser_or(array_new(), parser_or(cons_call(), res))) } let semicolon = ignore_whitespace(semicolon_sign) let comma = ignore_whitespace(comma_sign) let with_semicolon = (p:Coroutine) => p.then(p_res => ignore_whitespace(semicolon_sign).then(_ => co_unit(p_res))) export let type_args = (check_array_decl=true) : Coroutine> => parser_or>( type_decl(check_array_decl).then(a => parser_or>( comma.then(_ => type_args(check_array_decl).then(as => co_unit([a, ...as]))), co_unit([a]))), co_unit(Array())) let assign_right = () : Parser => no_match.then(_ => equal_sign.then(_ => partial_match.then(_ => expr().then(r => full_match.then(_ => co_unit(r) ))))) let assign : () => Parser = () => expr().then(l => assign_right().then(r => co_unit(mk_assign(l,r)))) let tuple_or_record : Coroutine = left_bracket.then(lb => parser_or(type_args().then(as => right_bracket.then(rb => co_unit(mk_tuple_type_decl(join_source_ranges(lb, rb), as)))), arg_decls().then(as => right_bracket.then(rb => co_unit(mk_record_type_decl(join_source_ranges(lb, rb), as)))) ) ) let array = (t:ParserRes) => left_square_bracket.then(_ => partial_match.then(_ => right_square_bracket.then(end_range => co_unit(mk_array_decl(join_source_ranges(t.range, end_range), t)) ))) let type_decl = (check_array_decl=true) : Coroutine => parser_or( check_array_decl ? parser_or( tuple_or_record.then(t => array(t) ), tuple_or_record) : tuple_or_record, identifier.then(i => parser_or( (check_array_decl ? array(i) : co_error({ range:zero_range, priority: -1, message:"" })), parser_or( lt_op.then(_ => partial_match.then(_ => type_args().then(args => gt_op.then(end_range => co_unit(mk_generic_type_inst(join_source_ranges(i.range, end_range), i, args)) )))), check_array_decl ? parser_or( left_square_bracket.then(_ => partial_match.then(_ => right_square_bracket.then(end_range => co_unit(mk_array_decl(join_source_ranges(i.range, end_range), i)) ))), co_unit(i)) : co_unit(i) )))) let decl_init : () => Coroutine = () => no_match.then(_ => type_decl().then(l => identifier.then(r => partial_match.then(_ => assign_right().then(v => full_match.then(_ => co_unit(mk_decl_and_init(l, r, v)))))))) let decl : () => Coroutine = () => no_match.then(_ => type_decl().then(l => identifier.then(r => partial_match.then(_ => co_unit(mk_decl(l, r)))))) let actuals = () : Coroutine> => parser_or>( expr().then(a => parser_or>( comma.then(_ => actuals().then(as => co_unit([a, ...as]))), co_unit([a]))), co_unit(Array())) let identifiers = () : Coroutine => parser_or>( identifier.then(a => parser_or>( comma.then(_ => identifiers().then(as => co_unit([a, ...as]))), co_unit([a]))), co_unit(Array())) let generic_identifiers = () : Coroutine => parser_or>( parser_or( identifier.then(i => lt_op.then(_ => generic_identifiers().then(args => gt_op.then(gt => co_unit(mk_generic_type_inst(join_source_ranges(i.range, gt), i, args)) )))), identifier).then(a => parser_or>( comma.then(_ => generic_identifiers().then(as => co_unit([a, ...as]))), co_unit([a]))), co_unit(Array())) let arg_decls = () : Coroutine> => parser_or>( decl().then(d => parser_or>( comma.then(_ => arg_decls().then(ds => co_unit([d, ...ds]))), co_unit([d]))), co_unit(Array())) let return_statement : () => Parser = () => no_match.then(_ => return_sign.then(return_range => partial_match.then(_ => parser_or( expr().then(e => co_unit(mk_return(e, join_source_ranges(return_range, e.range)))) , co_unit(mk_unit(return_range)) )))) let if_conditional : (_:() => Parser) => Parser = (stmt:() => Parser) => no_match.then(_ => if_keyword.then(if_keyword => partial_match.then(_ => expr().then(c => parser_or( left_curly_bracket.then(_ => un_bracketized_statement().then(t => right_curly_bracket.then(t_r_cb => parser_or( else_keyword.then(_ => left_curly_bracket.then(_ => un_bracketized_statement().then(e => right_curly_bracket.then(e_r_cb => full_match.then(_ => co_unit(mk_if_then_else(join_source_ranges(if_keyword, e_r_cb),c, t, e))))))), co_unit(mk_if_then(join_source_ranges(if_keyword, t_r_cb),c, t)))))), stmt().then(t => parser_or( else_keyword.then(_ => stmt().then(e => full_match.then(_ => co_unit(mk_if_then_else(join_source_ranges(if_keyword, e.range),c, t, e))))), co_unit(mk_if_then(join_source_ranges(if_keyword, t.range),c, t)))), ))))) let for_loop : (_:() => Parser) => Parser = (stmt:(ignore_semicolon?:boolean) => Parser) => no_match.then(_ => for_keyword.then(for_keyword_range => partial_match.then(_ => left_bracket.then(lb => stmt().then(i => expr().then(c => semicolon.then(_ => stmt(true).then(s => right_bracket.then(_ => left_curly_bracket.then(_ => un_bracketized_statement().then(b => right_curly_bracket.then(r_b => full_match.then(_ => co_unit(mk_for(join_source_ranges(for_keyword_range, r_b), i, c, s, b))))))))))))))) let while_loop : (_:() => Parser) => Parser = (stmt:() => Parser) => no_match.then(_ => while_keyword.then(while_keyword_range => partial_match.then(_ => expr().then(c => left_curly_bracket.then(_ => un_bracketized_statement().then(b => right_curly_bracket.then(r_b => full_match.then(_ => co_unit(mk_while(join_source_ranges(while_keyword_range, r_b), c, b)))))))))) let bracketized_statement = () => no_match.then(_ => left_curly_bracket.then(l_b_r => partial_match.then(_ => function_statements(co_lookup(right_curly_bracket).then(_ => co_unit({}))).then(s => right_curly_bracket.then(r_b_r => full_match.then(_ => co_unit({...s, range:join_source_ranges(l_b_r, r_b_r)}))))))) let un_bracketized_statement = () => no_match.then(_ => partial_match.then(_ => function_statements(co_lookup(right_curly_bracket).then(_ => co_unit({}))).then(s => full_match.then(_ => co_unit(s))))) let constructor_body = (function_name:{id: string;range: SourceRange}, arg_decls:DeclAST[], params_base_call:Option) => left_curly_bracket.then(_ => function_statements(co_lookup(right_curly_bracket).then(_ => co_unit({}))).then(body => right_curly_bracket.then(rb => full_match.then(_ => co_unit(mk_constructor_declaration(join_source_ranges(function_name.range, rb), function_name.id, Immutable.List(arg_decls), params_base_call, body)))))) let constructor_declaration = () => no_match.then(_ => identifier_token.then(function_name => left_bracket.then(_ => partial_match.then(_ => arg_decls().then(arg_decls => right_bracket.then(_ => parser_or( colon_keyword.then(_ => base.then(_ => left_bracket.then(_ => partial_match.then(_ => actuals().then(actuals => right_bracket.then(_ => { let args = actuals.length == 1 && actuals[0].ast.kind == "unit" ? [] : actuals.length == 1 && actuals[0].ast.kind == "," ? comma_to_array(actuals[0]) : actuals return constructor_body(function_name, arg_decls, apply(inl(),args)) })))))), constructor_body(function_name, arg_decls, apply(inr(),{})) ) )))))) let function_declaration = (modifiers:{range:SourceRange, ast:ModifierAST}[]) => no_match.then(_ => type_decl().then(return_type => identifier_token.then(function_name => generic_parameters().then(gps => left_bracket.then(_ => partial_match.then(_ => arg_decls().then(arg_decls => right_bracket.then(_ => parser_or( semicolon.then(semicolon => full_match.then(_ => co_unit(mk_function_declaration(join_source_ranges(return_type.range, semicolon), return_type, function_name.id, Immutable.List(arg_decls), {range:semicolon, ast:{ kind: "noop" }}, gps.toArray().map(p => p.name))))), left_curly_bracket.then(_ => function_statements(co_lookup(right_curly_bracket).then(_ => co_unit({}))).then(body => right_curly_bracket.then(rb => full_match.then(_ => co_unit(mk_function_declaration(join_source_ranges(return_type.range, rb), return_type, function_name.id, Immutable.List(arg_decls), body, gps.toArray().map(p => p.name)))))))))))))))) let class_body = (class_name:{id: string;range: SourceRange}, initial_range:SourceRange, generic_parameters:{ name:ParserRes, variant:"co"|"contra"|"inv" }[], extends_or_implements:{C_name:string, generic_parameters:GenericParameter[], ast:ParserRes}[], modifiers:Immutable.List) => left_curly_bracket.then(_ => class_statements().then(declarations => right_curly_bracket.then(closing_curly_range => full_match.then(_ => co_unit( mk_class_declaration( class_name.id, generic_parameters, extends_or_implements, declarations.fst, declarations.snd.fst.filter(m => m!=undefined && m.decl.generic_parameters.length == 0).toList(), declarations.snd.fst.filter(m => m!=undefined && m.decl.generic_parameters.length > 0).toList(), declarations.snd.snd, modifiers, join_source_ranges(initial_range, closing_curly_range)) ))))) let class_declaration = () => no_match.then(_ => class_modifiers().then(c_ms => class_keyword.then(initial_range => partial_match.then(_ => identifier_token.then(class_name => parser_or>( lt_op.then(_ => partial_match.then(_ => type_args(false).then(args => gt_op.then(end_range => co_unit(args) )))),co_unit(Array())).then(type_params => { let range = c_ms.count() == 0 ? initial_range : c_ms.toArray().map(c_m => c_m.range).reduce((p,n) => join_source_ranges(p,n)) return parser_or( colon_keyword.then(_ => generic_identifiers().then(extends_or_implements => class_body(class_name, range, type_params.map(p => ({ name:p, variant:"inv" as "inv"})), extends_or_implements_AUX(extends_or_implements), Immutable.List(c_ms.toArray().map(m => m.ast))) )), class_body(class_name, range, type_params.map(p => ({ name:p, variant:"inv" as "inv"})), [], Immutable.List(c_ms.toArray().map(m => m.ast))) )})))))) let extends_or_implements_AUX = (extends_or_implements:ParserRes[]) : {C_name:string, generic_parameters:GenericParameter[], ast:ParserRes}[] => { return extends_or_implements.map(i => { if(i.ast.kind == "id"){ return { C_name : i.ast.value, generic_parameters : [], ast:i } } if(i.ast.kind == "generic type inst" && i.ast.f.ast.kind == "id"){ return { C_name : i.ast.f.ast.value, generic_parameters : i.ast.args.map(e => (e.ast.kind == "id" ? { name:e.ast.value, variance:"inv" as "inv" } : { name:"?", variance:"inv" as "inv" })), ast:i } } return { C_name : "", generic_parameters : [], ast:i } }) } let interface_declaration = () => no_match.then(_ => class_modifiers().then(_ms => partial_match.then(_ => identifier_token.then(class_name => parser_or>( lt_op.then(_ => partial_match.then(_ => type_args().then(args => gt_op.then(end_range => co_unit(args) )))),co_unit(Array())).then(type_params => { let range = _ms.count() == 0 ? class_name.range : _ms.toArray().map(c_m => c_m.range).reduce((p,n) => join_source_ranges(p,n)) let _ms_new = _ms.toArray().some(m => m.ast.kind == "public") || _ms.count() == 0 ? _ms.toArray().map(m => m && m.ast) : _ms.toArray().map(m => m && m.ast).concat([{kind:"public"}]) return parser_or( colon_keyword.then(_ => generic_identifiers().then(extends_or_implements => class_body(class_name, range, type_params.map(p => ({ name:p, variant:"inv" as "inv"})), extends_or_implements_AUX(extends_or_implements), Immutable.List(_ms_new)) )), class_body(class_name, range, type_params.map(p => ({ name:p, variant:"inv" as "inv"})), [], Immutable.List(_ms_new)) )}))))) let outer_statement : () => Parser = () => parser_or( function_declaration([]).then(fun_decl => co_unit({ range: fun_decl.range, ast:fun_decl })), parser_or(class_declaration(), parser_or(interface_declaration(), inner_statement()))) let unchanged = CCC.id>().f let inner_statement = (skip_semicolon?:boolean) : Parser => parser_or(with_semicolon(co_unit(mk_noop())), parser_or(bracketized_statement(), parser_or(for_loop(function_statement), parser_or(while_loop(function_statement), parser_or(if_conditional(function_statement), parser_or((skip_semicolon ? unchanged : with_semicolon)(decl().then(d => co_unit({ range:join_source_ranges(d.l.range, d.r.range), ast:d }))), parser_or((skip_semicolon ? unchanged : with_semicolon)(decl_init().then(d_i => co_unit({ range:join_source_ranges(d_i.l.range, d_i.v.range), ast:d_i }))), parser_or((skip_semicolon ? unchanged : with_semicolon)(assign()), parser_or((skip_semicolon ? unchanged : with_semicolon)(expr()), parser_or((skip_semicolon ? unchanged : with_semicolon)(no_match.then(_ => dbg)), with_semicolon(no_match.then(_ => tc_dbg)) )))))))))) let function_statement = (skip_semicolon?:boolean) : Parser => parser_or(with_semicolon(return_statement()),inner_statement(skip_semicolon)) let generic_statements = (stmt: () => Parser, check_trailer: Coroutine) : Parser => parser_or( stmt().then(l => parser_or( generic_statements(stmt, check_trailer).then(r => co_unit(r.ast.kind == "noop" ? l : mk_semicolon(l, r))), check_trailer.then(_ => co_unit(l))) ), co_unit(mk_noop()) ) let function_statements = (check_trailer: Coroutine) : Parser => generic_statements (function_statement, check_trailer) let inner_statements = (check_trailer: Coroutine) : Parser => generic_statements (() => inner_statement(), check_trailer) let outer_statements = (check_trailer: Coroutine) : Parser => generic_statements (outer_statement, check_trailer) let modifier = () : Coroutine => parser_or<{ range:SourceRange, ast:ModifierAST }>(private_modifier.then(r => co_unit(mk_private(r))), parser_or<{ range:SourceRange, ast:ModifierAST }>(public_modifier.then(r => co_unit(mk_public(r))), parser_or<{ range:SourceRange, ast:ModifierAST }>(protected_modifier.then(r => co_unit(mk_protected(r))), parser_or<{ range:SourceRange, ast:ModifierAST }>(virtual_modifier.then(r => co_unit(mk_virtual(r))), parser_or<{ range:SourceRange, ast:ModifierAST }>(override_modifier.then(r => co_unit(mk_override(r))), parser_or<{ range:SourceRange, ast:ModifierAST }>(abstract_modifier.then(r => co_unit(mk_abstract(r))), parser_or<{ range:SourceRange, ast:ModifierAST }>(interface_modifier.then(r => co_unit(mk_interface(r))), static_modifier.then(r => co_unit(mk_static(r)))))))))) let generic_parameters = () : Coroutine> => parser_or>( lt_op.then(_ => type_args().then(args => gt_op.then(_ => co_unit(Immutable.List(args.map(a => ({name:a, variant:"inv" as "co"|"contra"|"inv"})))) ))), co_unit(Immutable.List()) ) let modifiers = () : Coroutine> => parser_or>( modifier().then(m => modifiers().then(ms => m.ast.kind == "private" && ms.some(m => !m || m.ast.kind == "public") || m.ast.kind == "public" && ms.some(m => !m || m.ast.kind == "private") || m.ast.kind == "virtual" && ms.some(m => !m || m.ast.kind == "override") || m.ast.kind == "override" && ms.some(m => !m || m.ast.kind == "virtual") || m.ast.kind == "abstract" && ms.some(m => !m || m.ast.kind == "virtual") ? co_get_state().then(s => co_error({ range:m.range, priority:s.branch_priority, message:"Error: incompatible modifiers." })) : co_unit(ms.push(m)))), co_unit(Immutable.List<{ range:SourceRange, ast:ModifierAST }>())) let class_modifiers = () : Coroutine> => parser_or>( modifier().then(m => modifiers().then(ms => m.ast.kind == "private" && ms.some(m => !m || m.ast.kind == "public") || m.ast.kind == "public" && ms.some(m => !m || m.ast.kind == "private") || m.ast.kind == "interface" && ms.some(m => !m || m.ast.kind == "abstract") || m.ast.kind == "abstract" && ms.some(m => !m || m.ast.kind == "interface") ? co_get_state().then(s => co_error({ range:m.range, priority:s.branch_priority, message:"Error: incompatible modifiers." })) : co_unit(ms.push(m)))), co_unit(Immutable.List<{ range:SourceRange, ast:ModifierAST }>())) let class_statements : () => Coroutine, Prod, Immutable.List>>> = () => parser_or, Prod, Immutable.List>>>( parser_or>>( with_semicolon(modifiers().then(ms => parser_or( decl_init().then(d => co_unit(d)), decl().then(d => co_unit(d))).then(d => co_unit>>( apply(inl>(), { decl:d, modifiers:ms }))))) , parser_or>>( modifiers().then(ms => function_declaration(ms.toArray()).then(d => co_unit>>( apply(inr>().after(inl()), { decl:d, modifiers:ms })))) , modifiers().then(ms => constructor_declaration().then(d => co_unit>>( apply(inr>().after(inr()), { decl:d, modifiers:ms })))) ) ).then(decl => class_statements().then(decls => co_unit, Prod, Immutable.List>>>({ fst:decl.kind == "left" ? decls.fst.push(decl.value) : decls.fst, snd:decl.kind == "right" ? decl.value.kind == "left" ? {...decls.snd, fst:decls.snd.fst.push(decl.value.value)} : {...decls.snd, snd:decls.snd.snd.push(decl.value.value)} : decls.snd }) )), co_lookup(right_curly_bracket).then(_ => co_unit, Prod, Immutable.List>>>({ fst:Immutable.List(), snd:{ fst:Immutable.List(), snd:Immutable.List() } }))) export let program : Parser = outer_statements(co_lookup(eof).then(_ => co_unit({}))).then(s => eof.then(_ => co_unit(s))) export let program_prs : () => Parser = () => parser_or( filesystem_prs.then(fs => program.then(prg => co_unit({ range: join_source_ranges(fs.range, prg.range), ast: mk_filesys_and_program_ast(fs, prg) }))), program )