[Geany] Scoped Ruby declarations, with a hackish patch

2012-09-06 Thread Colomban Wendling
Hi guys,

I saw that the ruby parser don't properly generate tags declarations like:

class Foo::Bar
end

which should generate a tag Bar with the scope Foo;  but it
generates a tag Foo and simply ignores Bar.  This seems to apply to
modules, classes and methods at least -- almost everything.

So I wanted to fix that.  Unfortunately the scoping code in CTags don't
really support to easily put several scopes at the same level, e.g. if
you want to push several scope you gotta handle the popping yourself.
And since there is one single block end, it's tricky to do.

Since I was way too lazy (and didn't even found a good implementation)
to fix that, I just did it the dirty way: reading the whole Foo::Bar
as a single tag name (Foo.Bar) and tuning the code registering the tag
to split this on the last ., putting the left part (if any) in the
scope.  Patch attached.  This is quite dirty, but works fine unless a
legitimate tag may include a . in its name, which doesn't seem the
case currently looking at the parser.

Note that Ruby isn't the only language that allows such kind of scoping.
 For example, Vala allows to prefix stuff with a namespace -- and there
is the same problem here.

So, especially Nick, what do you guys think of this?  Is this patch too
dirty?  Do somebody have a better idea?  Or is this too dirty and we
don't care because nobody writes ruby anyway?  In one word: opinions?

Thanks,
Colomban
From f0da754670cca4d4c3ddd0c8d7295881372ba3b6 Mon Sep 17 00:00:00 2001
From: Colomban Wendling b...@herbesfolles.org
Date: Thu, 6 Sep 2012 20:45:57 +0200
Subject: [PATCH] Correctly parse Ruby declarations with inline scoping

Generate the appropriate tags for declarations like:

	class Foo::Bar::Baz
	end

The implementation here is quite hackish but works fairly reliably.
---
 tagmanager/ctags/ruby.c |   38 --
 1 file changed, 36 insertions(+), 2 deletions(-)

diff --git a/tagmanager/ctags/ruby.c b/tagmanager/ctags/ruby.c
index a614eb1..c7695ef 100644
--- a/tagmanager/ctags/ruby.c
+++ b/tagmanager/ctags/ruby.c
@@ -132,11 +132,26 @@ static void emitRubyTag (vString* name, rubyKind kind)
 {
 	tagEntryInfo tag;
 	vString* scope;
+	const char *this_name;
 
 	vStringTerminate (name);
 	scope = stringListToScope (nesting);
 
-	initTagEntry (tag, vStringValue (name));
+	/* extract scope and actual name from tag name in case of tags like
+	 * class Foo::Bar::Baz which are parsed as a single name, Foo.Bar.Baz */
+	this_name = strrchr (vStringValue (name), '.');
+	if (this_name)
+	{
+		if (vStringLength (scope)  0)
+			vStringPut (scope, '.');
+		vStringNCat (scope, name, this_name - vStringValue (name));
+		vStringTerminate (scope);
+		this_name ++;
+	}
+	else
+		this_name = vStringValue (name);
+
+	initTagEntry (tag, this_name);
 	if (vStringLength (scope)  0) {
 	tag.extensionFields.scope [0] = class;
 	tag.extensionFields.scope [1] = vStringValue (scope);
@@ -230,7 +245,26 @@ static void readAndEmitTag (const unsigned char** cp, rubyKind expected_kind)
 	if (isspace (**cp))
 	{
 		vString *name = vStringNew ();
-		rubyKind actual_kind = parseIdentifier (cp, name, expected_kind);
+		vString *chunk = vStringNew ();
+		rubyKind actual_kind;
+		unsigned int i = 0;
+
+		/* parse the identifier, allowing scoping like class Foo::Bar::Baz */
+		while (1)
+		{
+			actual_kind = parseIdentifier (cp, chunk, expected_kind);
+			if (i++  0)
+vStringPut (name, '.');
+			vStringCat (name, chunk);
+			vStringClear (chunk);
+
+			if (actual_kind != K_UNDEFINED  (*cp)[0] == ':'  (*cp)[1] == ':')
+*cp += 2;
+			else
+break;
+		}
+		vStringDelete (chunk);
+		vStringTerminate (name);
 
 		if (actual_kind == K_UNDEFINED || vStringLength (name) == 0)
 		{
-- 
1.7.10.4

___
Geany mailing list
Geany@uvena.de
https://lists.uvena.de/cgi-bin/mailman/listinfo/geany


Re: [Geany] Scoped Ruby declarations, with a hackish patch

2012-09-06 Thread Colomban Wendling
Le 06/09/2012 20:58, Colomban Wendling a écrit :
 Hi guys,
 
 I saw that the ruby parser don't properly generate tags declarations like:
 
   class Foo::Bar
   end
 
 which should generate a tag Bar with the scope Foo;  but it
 generates a tag Foo and simply ignores Bar.  This seems to apply to
 modules, classes and methods at least -- almost everything.
 
 So I wanted to fix that.  Unfortunately the scoping code in CTags don't
 really support to easily put several scopes at the same level, e.g. if
 you want to push several scope you gotta handle the popping yourself.
 And since there is one single block end, it's tricky to do.
 
 Since I was way too lazy (and didn't even found a good implementation)
 to fix that, I just did it the dirty way: reading the whole Foo::Bar
 as a single tag name (Foo.Bar) and tuning the code registering the tag
 to split this on the last ., putting the left part (if any) in the
 scope.  Patch attached.

Ah, I forgot to mention that it'd be awesome if some true Ruby users
could test the patch and check whether it breaks anything I didn't see :)
___
Geany mailing list
Geany@uvena.de
https://lists.uvena.de/cgi-bin/mailman/listinfo/geany


Re: [Geany] Scoped Ruby declarations, with a hackish patch

2012-09-06 Thread Lex Trotman
On 7 September 2012 04:58, Colomban Wendling lists@herbesfolles.org wrote:
 Hi guys,

 I saw that the ruby parser don't properly generate tags declarations like:

 class Foo::Bar
 end

 which should generate a tag Bar with the scope Foo;  but it
 generates a tag Foo and simply ignores Bar.  This seems to apply to
 modules, classes and methods at least -- almost everything.

 So I wanted to fix that.  Unfortunately the scoping code in CTags don't
 really support to easily put several scopes at the same level, e.g. if
 you want to push several scope you gotta handle the popping yourself.
 And since there is one single block end, it's tricky to do.

 Since I was way too lazy (and didn't even found a good implementation)
 to fix that, I just did it the dirty way: reading the whole Foo::Bar

This is of course what C++ does when the declarations are not visible eg

a::b::f(){}; makes a function 'a::b::f' if the declarations of a and b
are not visible (eg in a closed .hpp file), if they are visible it
puts f inside the declaration of b. Compare the symbols pane entrys
for the definitions of f() and g() in the attached.

I'm not a rubyist but I assume that Foo doesn't need to be declared
before Bar in the above example and thats the problem.  From the C++
example what you would need to do is autodeclare Foo (as what?) so
you had somewhere to put Bar.

 as a single tag name (Foo.Bar) and tuning the code registering the tag
 to split this on the last ., putting the left part (if any) in the
 scope.  Patch attached.  This is quite dirty, but works fine unless a
 legitimate tag may include a . in its name, which doesn't seem the
 case currently looking at the parser.

 Note that Ruby isn't the only language that allows such kind of scoping.
  For example, Vala allows to prefix stuff with a namespace -- and there
 is the same problem here.

But it is statically declared so it should work like C++ should it not?

Cheers
Lex


 So, especially Nick, what do you guys think of this?  Is this patch too
 dirty?  Do somebody have a better idea?  Or is this too dirty and we
 don't care because nobody writes ruby anyway?  In one word: opinions?

 Thanks,
 Colomban

 ___
 Geany mailing list
 Geany@uvena.de
 https://lists.uvena.de/cgi-bin/mailman/listinfo/geany

namespace x {
	class y {
		void f();
	};
};

void x::y::f(){};

void a::b::g(){};

___
Geany mailing list
Geany@uvena.de
https://lists.uvena.de/cgi-bin/mailman/listinfo/geany


Re: [Geany] Scoped Ruby declarations, with a hackish patch

2012-09-06 Thread Colomban Wendling
Le 07/09/2012 00:49, Lex Trotman a écrit :
 On 7 September 2012 04:58, Colomban Wendling lists@herbesfolles.org 
 wrote:
 Hi guys,

 I saw that the ruby parser don't properly generate tags declarations like:

 class Foo::Bar
 end

 which should generate a tag Bar with the scope Foo;  but it
 generates a tag Foo and simply ignores Bar.  This seems to apply to
 modules, classes and methods at least -- almost everything.

 So I wanted to fix that.  Unfortunately the scoping code in CTags don't
 really support to easily put several scopes at the same level, e.g. if
 you want to push several scope you gotta handle the popping yourself.
 And since there is one single block end, it's tricky to do.

 Since I was way too lazy (and didn't even found a good implementation)
 to fix that, I just did it the dirty way: reading the whole Foo::Bar
 
 This is of course what C++ does when the declarations are not visible eg
 
 a::b::f(){}; makes a function 'a::b::f' if the declarations of a and b
 are not visible (eg in a closed .hpp file), if they are visible it
 puts f inside the declaration of b. Compare the symbols pane entrys
 for the definitions of f() and g() in the attached.

I don't think you got what I meant.  I'm not talking about what you see
in the symbol list, I'm talking about how the tags get parsed.  Of
course the symbol list shows something like A::B::C if A and B are not
declared, but that's just because symbol C has scope A::B and we display
the scope if we can't display the actual parent.

OK, maybe I could take a look at how the C++ parser parses A::B::C,
maybe they have a better solution;  but what I'm saying is that the
parser first reads identifiers A, B and C and put them in a single
string (with a separator of course) as if it was the identifier itself,
and splits it back later in A::B and C and uses the first as an
additional scope and the last as the symbol name.  This means that if
the separator token (:: in the example) can appear in a legitimate,
non-scoped identifier, it'd break.  And the code is quite ugly since it
first packs to later unpack.

A nicer approach would perhaps be to have a separation between blocks
and scope, e.g. a stack of blocks each accepting a multi-level scope,
instead of assuming block level == scope level.  Something like:

struct Block {
char *scope[];
struct Block children[];
}

thus the code:

namespace Foo {}
class Foo::Bar {
class Baz {}
}
method Foo::Bar::something {}

would give:

Block root = {
.scope = {}
.children = {
{
.scope = { Foo }
.children = {}
},
{
.scope = { Foo, Bar },
.children = {
{
.scope = { Baz },
.children = {}
}
}
},
{
.scope = { Foo, Bar, something },
.children = {}
}
}
}

Of course in practice we only need one block (with parents) at a time,
so we could simply use a two-dimensional array instead of such a complex
tree.  E.g. hierarchy inside class Baz would be:

{ { Foo, Bar }, { Baz } }

but if it was written like:

namespace Foo {
class Bar {
class Baz {}
}
}

the hierarchy would've been

{ { Foo }, { Bar }, { Baz } }

e.g. classic, where each block level corresponds to only one scope --
like it is now.

But again, I'm only talking about how the parser is written.

 I'm not a rubyist but I assume that Foo doesn't need to be declared
 before Bar in the above example and thats the problem.  From the C++
 example what you would need to do is autodeclare Foo (as what?) so
 you had somewhere to put Bar.

Nope, basic test shows that Ruby wants the levels to be declared, so
there is no need for auto-declaration.  However I think Vala's
namespaces needs the feature, e.g. the following:

namespace Foo.Bar {}

declares both namespaces Foo and Foo::Bar for that scope, and

class Foo.Bar () {}

declares namespace Foo and class Foo::Bar.

 as a single tag name (Foo.Bar) and tuning the code registering the tag
 to split this on the last ., putting the left part (if any) in the
 scope.  Patch attached.  This is quite dirty, but works fine unless a
 legitimate tag may include a . in its name, which doesn't seem the
 case currently looking at the parser.

 Note that Ruby isn't the only language that allows such kind of scoping.
  For example, 

Re: [Geany] Scoped Ruby declarations, with a hackish patch

2012-09-06 Thread Lex Trotman
[...]
 OK, maybe I could take a look at how the C++ parser parses A::B::C,
 maybe they have a better solution;  but what I'm saying is that the

Yeah, without much knowledge of Ruby or Vala I was just suggesting
that C++ *seemed* to work different so it might be worth a look
despite the convolutions of c.c.

Cheers
Lex

[...]

PS my quick look says c.c might use the same implementation as you
did, but I'm not sure, and am not going to try to follow the spaghetti
further ;)
___
Geany mailing list
Geany@uvena.de
https://lists.uvena.de/cgi-bin/mailman/listinfo/geany