X-Git-Url: http://nitlanguage.org diff --git a/src/parser_util.nit b/src/parser_util.nit index ec1d6e3..ff97a78 100644 --- a/src/parser_util.nit +++ b/src/parser_util.nit @@ -83,6 +83,20 @@ redef class ToolContext return nexpr end + # Parse a super class declaration + # Fatal error if the `string` is not a syntactically correct super class declaration + fun parse_superclass(string: String): ASuperclass + do + var mod_string = "class Dummy\nsuper {string}\nend" + var nclassdef = parse_classdef(mod_string).as(AStdClassdef) + var nsuperclasses = nclassdef.n_superclasses + if nsuperclasses.length != 1 then + self.fatal_error(null, "Fatal Error: not a super class declaration") + abort + end + return nsuperclasses.first + end + # Try to parse the `string` as something # # Returns the first possible syntacticaly correct type among: @@ -121,17 +135,18 @@ redef class ToolContext eof = tree.n_eof if not eof isa AError then var ntype = tree.n_base.n_classdefs.first.n_propdefs.first.as(AMethPropdef).n_block.as(ABlockExpr).n_expr.first.as(AVardeclExpr).n_type.n_types.first + ntype.parent = null return ntype end error = eof lexer = new Lexer(source) var first = lexer.next - if not first isa EOF then - var second = lexer.next - if second isa EOF and not second isa AError then - return first - end + if first isa EOF then return first + var second = lexer.next + if second isa EOF and not second isa AError then + first.parent = null + return first end lexer = new InjectedLexer(source) @@ -144,6 +159,7 @@ redef class ToolContext eof = tree.n_eof if not eof isa AError then var nexpr = tree.n_base.n_classdefs.first.n_propdefs.first.as(AMethPropdef).n_block.as(ABlockExpr).n_expr.first.as(AVardeclExpr).n_expr.as(AParExpr).n_expr + nexpr.parent = null return nexpr end if eof.location > error.location then error = eof @@ -156,7 +172,9 @@ redef class ToolContext tree = (new Parser(lexer)).parse eof = tree.n_eof if not eof isa AError then - var nblock = tree.n_base.n_classdefs.first.n_propdefs.first.as(AMethPropdef).n_block.as(ABlockExpr).n_expr.first.as(ADoExpr).n_block.as(not null) + var nblock = tree.n_base.n_classdefs.first.n_propdefs.first.as(AMethPropdef).n_block.as(ABlockExpr).n_expr.first.as(ADoExpr).n_block.as(ABlockExpr) + nblock.n_kwend = null # drop injected token + nblock.parent = null return nblock end if eof.location > error.location then error = eof @@ -180,7 +198,7 @@ redef class ToolContext loop printn prompt printn " " - var s = stdin.read_line + var s = sys.stdin.read_line if s == "" then continue if s.chars.first == ':' then var res = new TString @@ -215,7 +233,6 @@ class InjectedLexer do if not injected_before.is_empty then var tok = injected_before.shift - if tok._location == null then tok._location = new Location(file, 1, 1, 1, 0) return tok end if not is_finished then @@ -226,13 +243,12 @@ class InjectedLexer end var tok = injected_after.shift - if tok._location == null then tok._location = new Location(file, 1, 1, 1, 0) return tok end end redef class ANode - # Return an array of tokens that match a given text + # Do a deep search and return an array of tokens that match a given text fun collect_tokens_by_text(text: String): Array[Token] do var v = new CollectTokensByTextVisitor(text) @@ -240,7 +256,7 @@ redef class ANode return v.result end - # Return an array of node that are annotated + # Do a deep search and return an array of node that are annotated # The attached node can be retrieved by two invocation of parent fun collect_annotations_by_name(name: String): Array[AAnnotation] do