public static MULTI_TYPE parse(UNIT_REF first, iSCOPE context) { Debug.Indent(); Debug.WriteLine("Entering MULTI_TYPE.parse"); MULTI_TYPE multi = new MULTI_TYPE(); multi.add(first); Span begin = first.span; UNIT_REF elem_type = null; while (true) { Token token = get(); if (token.code != TokenCode.Vertical) { break; } forget(); elem_type = UNIT_REF.parse(null, false, context); multi.add(elem_type); } multi.setSpan(begin, elem_type.span); Debug.WriteLine("Exiting MULTI_TYPE.parse"); Debug.Unindent(); return(multi); }
public static new EXPRESSION parse(Token first, iSCOPE context) { EXPRESSION result; Token token = (first != null) ? first : get(); switch (token.code) { case TokenCode.Plus: case TokenCode.Minus: case TokenCode.Tilde: forget(); EXPRESSION second = UNARY.parse(null, context); result = new UNARY(token, second); result.setSpan(token.span, second.span); break; case TokenCode.New: forget(); UNIT_REF unitRef = UNIT_REF.parse(null, false, context); result = new NEW(unitRef); result.setSpan(token.span, unitRef.span); break; default: result = POWER.parse(token, context); break; } return(result); }
public static TYPE parseTypeSpecifier(iSCOPE context, out bool ref_val, out bool conc, out Span final) { TYPE type = null; bool opt = false; ref_val = false; conc = false; Token token = get(); Token begin = token; if (token.code == TokenCode.Question) { forget(); token = get(); opt = true; } switch (token.code) { case TokenCode.As: forget(); EXPRESSION example = PRIMARY.parse(null, context); type = example.type; if (type != null) { type.setSpan(begin.span, example.span); if (type is UNIT_REF) { (type as UNIT_REF).setSpecs(opt, true); } } final = example.span; break; case TokenCode.Ref: ref_val = true; forget(); goto ParseType; case TokenCode.Concurrent: conc = true; forget(); goto ParseType; case TokenCode.Val: forget(); goto ParseType; case TokenCode.LParen: // Seems to a tuple type type = TUPLE_TYPE.parse(context); context.add(type); final = type.span; break; default: // forget(); ParseType: UNIT_REF unitRef = UNIT_REF.parse(null, opt, context); if (unitRef == null) /* An error was detected earlier */ { final = null; return(null); } final = unitRef.span; token = get(); return(unitRef); } return(type); }
public static TYPE parse(iSCOPE context) { Debug.Indent(); Debug.WriteLine("Entering TYPE.parse"); Token token = get(); TYPE result = null; switch (token.code) { case TokenCode.LParen: result = TUPLE_TYPE.parse(context); break; case TokenCode.Routine: result = ROUTINE_TYPE.parse(context); break; case TokenCode.Identifier: { // Don't forget() UNIT_REF unit_type = UNIT_REF.parse(null, false, context); Token del = get(); if (del.code == TokenCode.Vertical) { // Don't call forget() result = MULTI_TYPE.parse(unit_type, context); } else { result = unit_type; } break; } default: // Syntax error { break; } } Debug.WriteLine("Exiting TYPE.parse"); Debug.Unindent(); return(result); }
public static void parse(iSCOPE context) { bool useConst = false; Token token = get(); Token begin = token; if (token.code == TokenCode.Const) { forget(); useConst = true; } while (true) { UNIT_REF ur = UNIT_REF.parse(null, false, context); USE result = new USE(ur, useConst); result.parent = context.self; result.setSpan(begin.span, ur.span); if (context is UNIT) { (context as UNIT).add(result); } else if (context is COMPILATION) { (context as COMPILATION).add(result); } // else // -- Some other use of 'use' token = get(); if (token.code != TokenCode.Comma) { break; } forget(); } }
/// <summary> /// /// </summary> /// <returns></returns> public static FORMAL_TYPE parse(Token id, iSCOPE context) { Debug.Indent(); Debug.WriteLine("Entering FORMAL_TYPE.parse"); // Identifier was parsed before and is passed via 'id'. FORMAL_TYPE generic_type_par = new FORMAL_TYPE(id); Token token = get(); if (token.code != TokenCode.Arrow2) { goto Finish; } // -> TYPE base_type = null; forget(); token = get(); if (token.code == TokenCode.Identifier) { forget(); base_type = UNIT_REF.parse(token, false, context); } else if (token.code == TokenCode.LParen) // T->(tuple) { base_type = TUPLE_TYPE.parse(context); } else { // Syntax error } generic_type_par.base_type = base_type; token = get(); if (token.code != TokenCode.Init) { goto Finish; } forget(); token = get(); // init if (token.code != TokenCode.LParen) { goto Finish; } forget(); token = get(); if (token.code == TokenCode.RParen) { forget(); goto Finish; } while (true) { TYPE init_param_type = TYPE.parse(context); generic_type_par.add(init_param_type); init_param_type.parent = generic_type_par; token = get(); if (token.code == TokenCode.Comma) { forget(); continue; } break; } token = expect(TokenCode.RParen); Finish: Debug.WriteLine("Exiting FORMAL_TYPE.parse"); Debug.Unindent(); generic_type_par.setSpan(id, token); return(generic_type_par); }
/// <summary> /// /// </summary> /// <syntax> /// UnitTypeName : CompoundName [ GenericInstantiation ] /// /// GenericInstantiation : "[" (Type|Expression) { "," (Type|Expression) } "]" /// </syntax> /// <returns></returns> public static UNIT_REF parse(Token id, bool opt, iSCOPE context) { Debug.Indent(); Debug.WriteLine("Entering UNIT_REF.parse"); Token token = null; // We assume that 'id' is 'identifier'. if (id == null) { token = get(); forget(); } else { token = id; } token = IDENTIFIER.parseCompoundName(token); if (token == null) /* an error was detected earlier */ return { (null); } Token start = token; UNIT_REF unit_ref = new UNIT_REF(token.image); unit_ref.opt = opt; unit_ref.as_sign = true; DECLARATION unit = Context.find(token); if (unit != null && (unit is UNIT || unit is FORMAL_TYPE)) { unit_ref.unit_ref = unit; } token = get(); if (token.code == TokenCode.LBracket) { forget(); while (true) { TYPE type = null; token = get(); if (token.code == TokenCode.LParen) { type = TUPLE_TYPE.parse(context); unit_ref.add(type); goto Delimiter; } EXPRESSION expr = EXPRESSION.parse(null, context); if (expr is REFERENCE || expr is UNRESOLVED) { string name = null; if (expr is REFERENCE) { if ((expr as REFERENCE).declaration is UNIT) { name = (expr as REFERENCE).declaration.name.identifier; } else { goto NonType; } } else // UNRESOLVED { name = (expr as UNRESOLVED).name.identifier; } id = new Token(expr.span, TokenCode.Identifier, name, new Category(CategoryCode.identifier)); type = UNIT_REF.parse(id, false, context); // Recursive call unit_ref.add(type); type.parent = unit_ref; goto Delimiter; } // else -- expr is perhaps a non-type argument NonType: token = get(); if (token.code == TokenCode.DotDot) { // This is actually a range _type_ forget(); EXPRESSION right = EXPRESSION.parse(null, context); RANGE_TYPE range = new RANGE_TYPE(expr, right); range.setSpan(expr.span, right.span); unit_ref.add(range); range.parent = unit_ref; } else // Definitely a non-type argument { unit_ref.add(expr); expr.parent = unit_ref; } Delimiter: token = get(); switch (token.code) { case TokenCode.Comma: forget(); continue; case TokenCode.RBracket: forget(); goto Finish; default: { /* Syntax error in generic actuals */ break; } } } Finish: unit_ref.setSpan(start.span, token.span); } else { unit_ref.setSpan(start); } Debug.WriteLine("Exiting UNIT_REF.parse"); Debug.Unindent(); return(unit_ref); }
/// <summary> /// /// </summary> /// <returns></returns> new public static TUPLE_TYPE parse(iSCOPE context) { Debug.Indent(); Debug.WriteLine("Entering TUPLE_TYPE.parse"); TUPLE_TYPE tuple = new TUPLE_TYPE(); Token begin = expect(TokenCode.LParen); int count = 0; Token token = get(); if (token.code == TokenCode.RParen) { // Empty tuple forget(); goto OutLoop; } var ids = new List <Token>(); UNIT_REF unit_type; while (true) { token = expect(TokenCode.Identifier); Token delimiter = get(); switch (delimiter.code) { case TokenCode.Comma: // Identifier is the current name in the list of fields forget(); ids.Add(token); continue; case TokenCode.Colon: // Identifier is the last name in the field of fields forget(); ids.Add(token); // Now we treat collected ids as names of tuple fields, // and the construct following ':' as unit name. unit_type = UNIT_REF.parse(null, false, context); unit_type.parent = tuple; foreach (Token id in ids) { if (tuple.exists(id.image)) // Error: duplicate name { } else { tuple.add(id.image, unit_type); } } token = get(); if (token.code == TokenCode.Is) { forget(); EXPRESSION expr = EXPRESSION.parse(null, context); foreach (Token id in ids) { if (tuple.exists(id.image)) { } else { tuple.add(id.image, expr); } } } ids.Clear(); break; case TokenCode.Is: ids.Add(token); // No explicit type for the field(s) but only initializer forget(); EXPRESSION expr2 = EXPRESSION.parse(null, context); foreach (Token id in ids) { if (tuple.exists(id.image)) // Error: duplicate name { } else { tuple.add(id.image, expr2); } } ids.Clear(); break; case TokenCode.RParen: case TokenCode.Semicolon: forget(); Token stop = delimiter; // ')': The end of the tuple type; this means that all previous ids were // actually simple type names. // ';': The end of the current part of the tuple type. Again, this means // that all ids were type names. // In both cases, field names were omitted, and we have to assign // artifical names for those fields. ids.Add(token); foreach (Token id in ids) { unit_type = UNIT_REF.parse(id, false, context); count++; string n = "$" + count.ToString(); tuple.add(n, unit_type); } ids.Clear(); if (stop.code == TokenCode.Semicolon) { continue; } else { goto OutLoop; } case TokenCode.LBracket: ids.Add(token); // 'ids' collected before, are actually unit names: // all of them before the last one, were simple names, // and the last one is like 'name[...'. foreach (Token id in ids) { unit_type = UNIT_REF.parse(id, false, context); count++; string n = "$" + count.ToString(); tuple.add(n, unit_type); } break; } // switch } // while OutLoop: tuple.setSpan(begin, token); Debug.WriteLine("Exiting TUPLE_TYPE.parse"); Debug.Unindent(); return(tuple); }
public static void parse(iSCOPE context) // REWRITE!! { Token token = get(); Token begin = token; if (token.code != TokenCode.Catch) // Compiler error { } forget(); CATCH handler = new CATCH(); Context.enter(handler); token = get(); if (token.code != TokenCode.LParen) // Syntax error { } else { forget(); } token = get(); if (token.code != TokenCode.Identifier) // Syntax error { } else { Token id = token; forget(); token = get(); if (token.code == TokenCode.Colon) { forget(); // handler.identifier = id.image; token = get(); if (token.code != TokenCode.Identifier) // Syntax error { } forget(); token = id; } UNIT_REF unit_ref = UNIT_REF.parse(null, false, context); // CHECK!! handler.unit_ref = unit_ref; unit_ref.parent = handler; } token = get(); if (token.code != TokenCode.RParen) // Syntax error { } forget(); BODY.parse(TokenCode.Catch, TokenCode.Else, TokenCode.End, handler); token = get(); // just to get the span... handler.setSpan(begin, token); Context.exit(); context.add(handler); }
/// <summary> /// /// </summary> /// <returns></returns> public static new EXPRESSION parse(Token first, iSCOPE context) { EXPRESSION result = null; Token token = (first == null) ? get() : first; Token begin = token; switch (token.code) { case TokenCode.This: forget(); UNIT unit = Context.unit(); if (unit == null) { // Error message! result = new THIS(null); } else { result = new THIS(unit); result.setSpan(token); } break; case TokenCode.Return: if (!ENTITY.weAreWithinEnsure) { break; } forget(); ROUTINE routine = Context.routine(); if (routine == null) { } // error else { result = new RETURN_EXPR(routine); result.setSpan(token); } break; case TokenCode.Old: forget(); result = EXPRESSION.parse(null, context); Span end = result.span; OLD old = new OLD(result); result.parent = old; result = old; result.setSpan(begin.span, end); break; case TokenCode.If: result = CONDITIONAL.parse(context); break; case TokenCode.LParen: forget(); Span start_tuple = token.span; result = EXPRESSION.parse(null, context); token = get(); if (token.code == TokenCode.Comma) { // Seems to be a tuple forget(); TUPLE_EXPR tuple = new TUPLE_EXPR(); tuple.add(result); while (true) { EXPRESSION expr = EXPRESSION.parse(null, context); tuple.add(expr); token = get(); if (token.code != TokenCode.Comma) { break; } forget(); } result = tuple; } end = expect(TokenCode.RParen).span; result.setSpan(token.span, end); break; case TokenCode.Identifier: if (first == null) { forget(); ////// perhaps the same condition should be added for all cases? } DECLARATION d = Context.find(token); if (d == null) { result = new UNRESOLVED(context, new IDENTIFIER(token)); } else { result = new REFERENCE(d); } Token token2 = get(); if (token2.code == TokenCode.LBracket) { UNIT_REF unitRef = UNIT_REF.parse(token, false, context); result = new NEW(unitRef); result.setSpan(unitRef.span); } else { result.setSpan(token); } break; case TokenCode.Integer: case TokenCode.Real: case TokenCode.String: case TokenCode.Character: result = new LITERAL(token.value, token.span, token.code); result.setSpan(token); forget(); break; default: return(null); } // result.setSpan(token); return(result); }