Skip to content

Commit

Permalink
Add SourceLoc.typ method, showing the name of the containing type.
Browse files Browse the repository at this point in the history
SourceLoc already shows the method name of the __loc object,
but not the type name. Adding the type name is a logical next step,
since it is available.

The method is named typ, because type is a reserved keyword that
cannot be used as a method name.
  • Loading branch information
jemc committed Apr 11, 2018
1 parent 84a9901 commit 8bcdf42
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 10 deletions.
6 changes: 5 additions & 1 deletion packages/builtin/source_loc.pony
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ interface val SourceLoc
Name and path of source file.
"""

fun typ(): String
"""
Name of containing class, actor, primitive, struct, interface, or trait.
"""

fun method(): String
"""
Name of containing method.
Expand All @@ -23,4 +28,3 @@ interface val SourceLoc
Character position on line.
Character positions start at 1.
"""

21 changes: 21 additions & 0 deletions src/libponyc/pass/sugar.c
Original file line number Diff line number Diff line change
Expand Up @@ -1065,6 +1065,21 @@ ast_t* expand_location(ast_t* location)
}
}

// Find name of containing type.
const char* type_name = "";
for(ast_t* typ = location; typ != NULL; typ = ast_parent(typ))
{
token_id variety = ast_id(typ);

if(variety == TK_INTERFACE || variety == TK_TRAIT ||
variety == TK_PRIMITIVE || variety == TK_STRUCT ||
variety == TK_CLASS || variety == TK_ACTOR)
{
type_name = ast_name(ast_child(typ));
break;
}
}

// Create an object literal.
BUILD(ast, location,
NODE(TK_OBJECT, DATA("__loc")
Expand All @@ -1077,6 +1092,12 @@ ast_t* expand_location(ast_t* location)
NONE
NODE(TK_SEQ, STRING(file_name))
NONE)
NODE(TK_FUN, AST_SCOPE
NODE(TK_TAG) ID("typ") NONE NONE
NODE(TK_NOMINAL, NONE ID("String") NONE NONE NONE)
NONE
NODE(TK_SEQ, STRING(type_name))
NONE)
NODE(TK_FUN, AST_SCOPE
NODE(TK_TAG) ID("method") NONE NONE
NODE(TK_NOMINAL, NONE ID("String") NONE NONE NONE)
Expand Down
24 changes: 15 additions & 9 deletions test/libponyc/sugar_expr.cc
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,7 @@ TEST_F(SugarExprTest, Location)
" fun box bar(): None =>\n"
" object\n"
" fun tag file(): String => \"\"\n"
" fun tag typ(): String => \"Foo\"\n"
" fun tag method(): String => \"bar\"\n"
" fun tag line(): USize => 4\n"
" fun tag pos(): USize => 5\n"
Expand All @@ -578,35 +579,40 @@ TEST_F(SugarExprTest, LocationDefaultArg)
const char* short_form =
"interface val SourceLoc\n"
" fun file(): String\n"
" fun typ(): String\n"
" fun method(): String\n"
" fun line(): USize\n"
" fun pos(): USize\n"

"class Foo\n"
" var create: U32\n"
" fun bar() =>\n"
" wombat()\n"
" fun wombat(x: SourceLoc = __loc) =>\n"
" None";
" Log.apply()"

"primitive Log\n"
" fun apply(x: SourceLoc = __loc) => None\n";

const char* full_form =
"interface val SourceLoc\n"
" fun file(): String\n"
" fun typ(): String\n"
" fun method(): String\n"
" fun line(): USize\n"
" fun pos(): USize\n"

"class Foo\n"
" var create: U32\n"
" fun bar() =>\n"
" wombat(object\n"
" Log.apply(object\n"
" fun tag file(): String => \"\"\n"
" fun tag typ(): String => \"Foo\"\n"
" fun tag method(): String => \"bar\"\n"
" fun tag line(): USize => 9\n"
" fun tag pos(): USize => 11\n"
" end)\n"
" fun wombat(x: SourceLoc = __loc) =>\n"
" None";
" fun tag line(): USize => 10\n"
" fun tag pos(): USize => 14\n"
" end)"

"primitive Log\n"
" fun apply(x: SourceLoc = __loc) => None\n";

TEST_EQUIV(short_form, full_form);
}
Expand Down

0 comments on commit 8bcdf42

Please sign in to comment.