Skip to content

Commit 7473e67

Browse files
committed
add unit tests for AST
1 parent 06eb87d commit 7473e67

13 files changed

Lines changed: 181 additions & 22 deletions

‎duchain/declarationbuilder.cpp‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -922,10 +922,11 @@ void DeclarationBuilder::visitLambda(LambdaAst* node)
922922
// A context must be opened, because the lamdba's arguments are local to the lambda:
923923
// d = lambda x: x*2; print x # <- gives an error
924924
openContext(node, editorFindRange(node, node->body), DUContext::Other);
925-
foreach ( ExpressionAst* argument, node->arguments->arguments ) {
925+
foreach ( ArgAst* argument, node->arguments->arguments ) {
926+
#warning this cannot work correctly
926927
// Create variable declarations for the lambda's arguments, so they aren't displayed as errors
927928
if ( argument->astType == Ast::NameAstType ) {
928-
visitVariableDeclaration<Declaration>(static_cast<NameAst*>(argument));
929+
// visitVariableDeclaration<Declaration>(static_cast<NameAst*>(argument));
929930
}
930931
}
931932
closeContext();
@@ -1629,9 +1630,15 @@ void DeclarationBuilder::visitArguments( ArgumentsAst* node )
16291630
int firstDefaultParameterOffset = parametersCount - defaultParametersCount;
16301631
int currentIndex = 0;
16311632
kDebug() << "variable argument ranges: " << node->arg_lineno << node->arg_col_offset << node->vararg_lineno << node->vararg_col_offset;
1633+
<<<<<<< HEAD
16321634
foreach ( ExpressionAst* expression, node->arguments ) {
16331635
// Iterate over all the function's arguments, create declarations, and add the arguments
16341636
// to the functions FunctionType.
1637+
=======
1638+
foreach ( ArgAst* arg, node->arguments ) {
1639+
ExpressionAst* expression = 0; //arg->argumentName;
1640+
#warning this cannot work
1641+
>>>>>>> add unit tests for AST
16351642
currentIndex += 1;
16361643
realParam = dynamic_cast<NameAst*>(expression);
16371644

‎parser/CMakeLists.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,4 @@ target_link_libraries(kdev4pythonparser
2323
add_dependencies(kdev4pythonparser parser)
2424
install(TARGETS kdev4pythonparser DESTINATION ${INSTALL_TARGETS_DEFAULT_ARGS})
2525

26+
add_subdirectory(tests)

‎parser/ast.cpp‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ ArgumentsAst::ArgumentsAst(Ast* parent): Ast(parent, Ast::ArgumentsAstType), arg
4343

4444
}
4545

46+
ArgAst::ArgAst(Ast* parent): Ast(parent, Ast::ArgAstType), argumentName(0), annotation(0)
47+
{
48+
49+
}
50+
4651
AssertionAst::AssertionAst(Ast* parent): StatementAst(parent, Ast::AssertionAstType)
4752
{
4853

‎parser/ast.h‎

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,7 @@
1919
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
2020
***************************************************************************/
2121

22-
<<<<<<< HEAD
23-
// The Python 2.7 Language Reference was used as basis for this AST
24-
=======
2522
// The Python 3.2 Language Reference was used as basis for this AST
26-
>>>>>>> adapt to new objects in python 3.2
2723

2824
#ifndef PYTHON_AST_H
2925
#define PYTHON_AST_H
@@ -134,8 +130,12 @@ class KDEVPYTHONPARSER_EXPORT Ast
134130
ComprehensionAstType,
135131
ExceptionHandlerAstType,
136132
AliasAstType, // for imports
133+
<<<<<<< HEAD
137134

138135
IdentifierAstType
136+
=======
137+
LastAstType // the largest one, not valid!
138+
>>>>>>> add unit tests for AST
139139
};
140140

141141
enum BooleanOperationTypes {
@@ -616,10 +616,18 @@ class KDEVPYTHONPARSER_EXPORT IndexAst : public SliceAstBase {
616616
};
617617

618618
/** Independent classes **/
619+
620+
class KDEVPYTHONPARSER_EXPORT ArgAst : public Ast {
621+
public:
622+
ArgAst(Ast* parent);
623+
Identifier* argumentName;
624+
ExpressionAst* annotation;
625+
};
626+
619627
class KDEVPYTHONPARSER_EXPORT ArgumentsAst : public Ast {
620628
public:
621629
ArgumentsAst(Ast* parent);
622-
QList<ExpressionAst*> arguments;
630+
QList<ArgAst*> arguments;
623631
QList<ExpressionAst*> defaultValues;
624632
Identifier* vararg;
625633
Identifier* kwarg;
@@ -634,13 +642,6 @@ class KDEVPYTHONPARSER_EXPORT KeywordAst : public Ast {
634642
ExpressionAst* value;
635643
};
636644

637-
class KDEVPYTHONPARSER_EXPORT ArgAst : public Ast {
638-
public:
639-
ArgAst(Ast* parent);
640-
Identifier* argumentName;
641-
ExpressionAst* annotation;
642-
};
643-
644645
class KDEVPYTHONPARSER_EXPORT ComprehensionAst : public Ast {
645646
public:
646647
ComprehensionAst(Ast* parent);

‎parser/astbuilder.cpp‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,7 @@ CodeAst* AstBuilder::parse(KUrl filename, QString& contents)
353353

354354
RangeUpdateVisitor v;
355355
v.visitNode(t.ast);
356+
free(homedir);
356357

357358
#ifdef _WIN32
358359
delete[] we;

‎parser/astdefaultvisitor.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ void AstDefaultVisitor::visitKeyword(KeywordAst* node)
375375

376376
void AstDefaultVisitor::visitArguments(ArgumentsAst* node)
377377
{
378-
foreach (ExpressionAst* expression, node->arguments) {
378+
foreach (ArgAst* expression, node->arguments) {
379379
visitNode(expression);
380380
}
381381
}

‎parser/conversionGenerator.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ def pluginAstToPythonAstType(plugintypestr):
9797
if plugintypestr == 'SliceAst': return '_slice'
9898
if plugintypestr == 'Ast': return '_stmt' # not sure about this
9999
if plugintypestr == 'GeneratorExpressionAst': return '_expr'
100+
if plugintypestr == 'ArgAst': return '_arg'
100101
else:
101102
sys.stderr.write("W: Could not decode name %s\n" % plugintypestr)
102103
return '<ERROR>'

‎parser/generated.h‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ class PythonAstTransformer {
3737
if ( ! node ) return nodelist;
3838
for ( int i=0; i < node->size; i++ ) {
3939
T* currentNode = static_cast<T*>(node->elements[i]);
40-
Q_ASSERT(currentNode);
4140
Ast* result = visitNode(currentNode);
4241
K* transformedNode = static_cast<K*>(result);
4342
nodelist.append(transformedNode);
@@ -682,7 +681,7 @@ v->isInt = PyLong_Check(node->v.Num.n);
682681
ArgumentsAst* v = new (m_pool->allocate(sizeof(ArgumentsAst))) ArgumentsAst(parent());
683682
v->vararg = node->vararg ? new (m_pool->allocate(sizeof(Python::Identifier))) Python::Identifier(PyUnicodeObjectToQString(node->vararg)) : 0;
684683
v->kwarg = node->kwarg ? new (m_pool->allocate(sizeof(Python::Identifier))) Python::Identifier(PyUnicodeObjectToQString(node->kwarg)) : 0;
685-
nodeStack.push(v); v->arguments = visitNodeList<_expr, ExpressionAst>(node->args); nodeStack.pop();
684+
nodeStack.push(v); v->arguments = visitNodeList<_arg, ArgAst>(node->args); nodeStack.pop();
686685
nodeStack.push(v); v->defaultValues = visitNodeList<_expr, ExpressionAst>(node->defaults); nodeStack.pop();
687686
v->arg_lineno = tline(node->arg_lineno - 1);
688687
v->arg_col_offset = node->arg_col_offset;

‎parser/python32.sdef‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ RULE_FOR _slice;KIND Index_kind;ACTIONS create|IndexAst set|value->ExpressionAst
6363

6464
RULE_FOR _comprehension;KIND any;ACTIONS create|ComprehensionAst set|target->ExpressionAst,target set|iterator->ExpressionAst,iter set|conditions=>ExpressionAst,ifs;;
6565
RULE_FOR _excepthandler;KIND ExceptHandler_kind;ACTIONS create|ExceptionHandlerAst set|type->ExpressionAst,type set|name~>name set|body=>Ast,body;;
66-
RULE_FOR _arguments;KIND any;ACTIONS create|ArgumentsAst set|vararg~>vararg set|kwarg~>kwarg set|arguments=>ExpressionAst,args set|defaultValues=>ExpressionAst,defaults
66+
RULE_FOR _arguments;KIND any;ACTIONS create|ArgumentsAst set|vararg~>vararg set|kwarg~>kwarg set|arguments=>ArgAst,args set|defaultValues=>ExpressionAst,defaults
6767
set|arg_linenol>arg_lineno set|arg_col_offset+>arg_col_offset set|vararg_linenol>vararg_lineno set|vararg_col_offset+>vararg_col_offset;;
6868
RULE_FOR _arg;KIND any;ACTIONS create|ArgAst set|argumentName~>arg set|annotation->ExpressionAst,annotation;;
6969
RULE_FOR _keyword;KIND any;ACTIONS create|KeywordAst set|argumentName~>arg set|value->ExpressionAst,value;;

‎parser/tests/CMakeLists.txt‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
kde4_add_unit_test(pyasttest pyasttest.cpp)
2+
target_link_libraries(pyasttest kdev4pythonparser ${QT_QTTEST_LIBRARY} ${KDEVPLATFORM_TESTS_LIBRARIES})

0 commit comments

Comments
 (0)