Developer forums (C::B DEVELOPMENT STRICTLY!) > Development
CC scope
ollydbg:
@eranif
Dear eranif, thanks for your direction. I guess that this has done in your Codelite IDE, it is in your Bison(yacc) based parser, right?
Yes, it can be implemented, I'm thinking how this mechanism can be added to our cc's parser. (c::b's parser should be extend to handle brace scope information, we don't have a separate parser to parse the function body, so it may be add code everywhere in the recent parser's code)
Alpha:
--- Quote from: eranif on October 30, 2011, 07:15:43 am ---
--- Quote ---I think the hard part with this method would how to keep track of which child node the user is currently typing in.
--- End quote ---
Its pretty simple actually. Assuming you know the location of the current function's body you should collect locals and manage them in a stack.
--- End quote ---
That makes sense (unless I have completely misunderstood you :)); I had been thinking along the lines of storing tokens, not re-parsing on cursor movements (except if made necessary by the user's edits).
(As you can tell, I still have a lot to learn about Code::Blocks' inner workings.)
eranif:
--- Quote --- it is in your Bison(yacc) based parser, right?
--- End quote ---
Actually, I don't parse locals until they are needed (e.g. user is trying to code complete something).
I wrote a lexer for this purpose (bison is not really needed here, I managed to do it with flex only)
http://codelite.svn.sourceforge.net/viewvc/codelite/trunk/ScopeOptimizer/ScopeOptimizer/scope_optimizer.l?revision=5183&view=markup
What this lexer does, its actually "optimize the scope", that means that it will remove all hidden blocks so the parser won't have to deal with them,
so this:
--- Code: ---int main(int argc,char **argv) {
{
int member;
}
int another_variable;
| // caret is here
--- End code ---
will become this:
--- Code: ---int main(int argc,char **argv) {
{}
int another_variable;
--- End code ---
note that this code is very independent (it comes with its own codelite workspace) and it expose a very simple API:
--- Code: ---int OptimizeScope(const std::string &inputScope, std::string &optimizedScope)
--- End code ---
The first argument is the buffer you want to optimize while the later is the result (the output).
Eran
ollydbg:
--- Quote from: eranif on October 30, 2011, 06:57:22 pm ---
--- Quote --- it is in your Bison(yacc) based parser, right?
--- End quote ---
Actually, I don't parse locals until they are needed (e.g. user is trying to code complete something).
--- End quote ---
Yes, Codeblocks' CC do the same thing as it needed(e.g. when user select context menu->find declaration of XXXXX).
--- Quote ---I wrote a lexer for this purpose (bison is not really needed here, I managed to do it with flex only)
http://codelite.svn.sourceforge.net/viewvc/codelite/trunk/ScopeOptimizer/ScopeOptimizer/scope_optimizer.l?revision=5183&view=markup
What this lexer does, its actually "optimize the scope", that means that it will remove all hidden blocks so the parser won't have to deal with them,
so this:
--- Code: ---int main(int argc,char **argv) {
{
int member;
}
int another_variable;
| // caret is here
--- End code ---
will become this:
--- Code: ---int main(int argc,char **argv) {
{}
int another_variable;
--- End code ---
note that this code is very independent (it comes with its own codelite workspace) and it expose a very simple API:
--- Code: ---int OptimizeScope(const std::string &inputScope, std::string &optimizedScope)
--- End code ---
The first argument is the buffer you want to optimize while the later is the result (the output).
Eran
--- End quote ---
Codeblocks' current implementation lacks such feature, In the currently CC, we just let the parser parse the function body(we say: buffer) until the caret. So, as your said, I think c::b's cc can improve this by modify the buffer, just like you mentioned. The, we can let our parser works in the modified buffer(optimized scope string as in your example).
That's really cool! thanks for the hint, so the last thing is to take some time to implement it. :D
Alpha:
Here is a possible scope optimizer for CC. If anyone has time to test, let me know how well it works.
--- Code: ---Index: src/plugins/codecompletion/nativeparser.cpp
===================================================================
--- src/plugins/codecompletion/nativeparser.cpp (revision 8571)
+++ src/plugins/codecompletion/nativeparser.cpp (working copy)
@@ -1826,7 +1826,7 @@
if (blockStart != -1)
{
++blockStart; // skip {
- const int pos = caretPos == -1 ? searchData->control->GetCurrentPos() : caretPos;
+ const int pos = (caretPos == -1 ? searchData->control->GetCurrentPos() : caretPos);
const int line = searchData->control->LineFromPosition(pos);
const int blockEnd = searchData->control->GetLineEndPosition(line);
if (blockEnd < 0 || blockEnd > searchData->control->GetLength())
@@ -1842,7 +1842,29 @@
if (blockStart >= blockEnd)
blockStart = blockEnd;
- wxString buffer = searchData->control->GetTextRange(blockStart, blockEnd);
+ wxString buffer; // = searchData->control->GetTextRange(blockStart, blockEnd);
+ // condense out-of-scope braces {...}
+ int scanPos = blockEnd;
+ for (int i = pos; i > blockStart; --i)
+ {
+ if (searchData->control->GetCharAt(i) != wxT('}'))
+ continue;
+ const int style = searchData->control->GetStyleAt(i);
+ if ( searchData->control->IsString(style)
+ || searchData->control->IsCharacter(style)
+ || searchData->control->IsComment(style))
+ {
+ continue;
+ }
+ const int scopeStart = searchData->control->BraceMatch(i);
+ if (scopeStart < blockStart)
+ break;
+ buffer.Prepend(searchData->control->GetTextRange(i, scanPos));
+ scanPos = scopeStart + 1;
+ i = scopeStart;
+ }
+ buffer.Prepend(searchData->control->GetTextRange(blockStart, scanPos));
+
buffer.Trim();
if ( !buffer.IsEmpty()
&& !m_Parser->ParseBuffer(buffer, false, false, true, searchData->file, m_LastFuncTokenIdx, initLine) )
--- End code ---
Navigation
[0] Message Index
[#] Next page
[*] Previous page
Go to full version