pyccel.parser.base module#
Module containing aspects of a parser which are in common over all stages.
- class pyccel.parser.base.BasicParser(verbose)[source]#
Bases:
object
Class for a basic parser.
This class contains functions and properties which are common to SyntacticParser and SemanticParser.
- Parameters:
verbose (int) – The level of verbosity.
See also
SyntacticParser
A parser for Pyccel based on a context-free grammar.
SemanticParser
A parser for Pyccel based on a context-sensitive grammar.
Examples
To use the BasicParser class, create an instance and call its parse() method: >>> parser = BasicParser() >>> result = parser.parse(“1 + 2”)
- property ast#
Abstract syntax tree.
Get the abstract syntax tree describing the code. This object contains PyccelAstNode objects and is generated by the syntactic stage. The abstract syntax tree is similar to the full syntax tree, but only contains information about the syntax, there is no semantic data (e.g. the types of variables are unknown).
- property blocking#
- property code#
The original code.
Get the original Python code which is being translated.
- create_new_class_scope(name, **kwargs)[source]#
Create a new scope for a Python class.
Create a new Scope object for a Python class with the given name, and attach any decorators’ information to the scope. The new scope is a child of the current one, and can be accessed from the dictionary of its children using the function name as key.
Before returning control to the caller, the current scope (stored in self._scope) is changed to the one just created, and the function’s name is stored in self._current_function_name.
- Parameters:
name (str) – Function’s name, used as a key to retrieve the new scope.
**kwargs (dict) – A dictionary containing any additional arguments of the new scope.
- Returns:
The scope for the class.
- Return type:
- property current_ast_node#
The AST for the current node.
The AST object describing the current node. This object is never set to None when entering a node. Therefore if a node has no AST object (e.g. a Variable) the current_ast_node will contain the AST of the enclosing object. It is set in the _visit method of SemanticParser. This object is useful for reporting errors on objects whose context is unknown (e.g. Variables).
- property current_function_name#
The name of the function currently being visited.
The name of the function currently being visited or None if we are not in a function.
- exit_function_scope()[source]#
Exit the function scope and return to the enclosing scope.
Exit the function scope and return to the enclosing scope.
- property filename#
The file being translated.
Get the name of the file being translated.
- property fst#
Full syntax tree.
Get the full syntax tree describing the code. This object contains PyccelAstNode objects and is generated by the semantic stage. The full syntax tree is similar to the abstract syntax tree, but additionally contains information about the types of the objects etc.
- insert_function(func, scope=None)[source]#
Insert a function into the current scope or a specified scope.
Insert a function into a scope under the final name by which it will be known in the generated code. The scope is the current scope unless another scope is provided. This is notably the case when dealing with class methods which are not inserted into the enclosing scope.
- Parameters:
func (FunctionDef | SympyFunction | Interface | FunctionAddress) – The function to be inserted into the scope.
scope (Scope, optional) – The scope where the function should be inserted.
- property is_header_file#
Indicate if the file being translated is a header file.
Indicate if the file being translated is a header file. A file is a header file if it does not include the implementation of the methods. This is the case for .pyi files.
- property metavars#
- property scope#
The Scope object containing all objects defined within the current scope
- property semantic_done#
- property syntax_done#
- pyccel.parser.base.get_filename_from_import(module_name, input_folder_name, output_folder_name)[source]#
Get the absolute path of a module_name, searching in a given folder.
Return a valid filename with an absolute path, that corresponds to the definition of module_name. When searching for files in a folder, the order of priority is:
python files (extension == .py)
header files (extension == .pyi)
In the Pyccel folder the priority is inverted as .py files are sometimes provided alongside .pyi files to spoof the functionalities so user code relying on these methods can be run in Python.
- Parameters:
module_name (str | AsName) – Name of the module_name of interest.
input_folder_name (str | Path) – Relative path of the folder which should be searched for the module_name.
output_folder_name (str | Path) – The name of the folder where the output of the translation of the module from which we are searching was printed.
- Returns:
filename (pathlib.Path) – Absolute path to the Python file being imported.
stashed_filename (pathlib.Path) – Absolute path to the .pyi version of the Python file being imported. If none exists then the absolute path to the Python file being imported.
- Raises:
PyccelError – Error raised when the module_name cannot be found. Error raised when the file imports a file that has not been translated. Error raised when the file imports a file that has been changed since its last translation.