pyccel.parser.base module#
Module containing aspects of a parser which are in common over all stages.
- class pyccel.parser.base.BasicParser(verbose)[source]View on GitHub#
Bases:
objectClass 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
SyntacticParserA parser for Pyccel based on a context-free grammar.
SemanticParserA 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 astView on GitHub#
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 blockingView on GitHub#
- property codeView on GitHub#
The original code.
Get the original Python code which is being translated.
- create_new_class_scope(name, **kwargs)[source]View on GitHub#
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:
- create_new_loop_scope()[source]View on GitHub#
Create a new scope describing a loop
- property current_ast_nodeView on GitHub#
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_nameView on GitHub#
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_class_scope()[source]View on GitHub#
Exit the class scope and return to the encasing scope
- exit_function_scope()[source]View on GitHub#
Exit the function scope and return to the enclosing scope.
Exit the function scope and return to the enclosing scope.
- exit_loop_scope()[source]View on GitHub#
Exit the loop scope and return to the encasing scope
- property filenameView on GitHub#
The file being translated.
Get the name of the file being translated.
- property fstView on GitHub#
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]View on GitHub#
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 | Interface | FunctionAddress) – The function to be inserted into the scope.
scope (Scope, optional) – The scope where the function should be inserted.
- property is_stub_fileView on GitHub#
Indicate if the file being translated is a stub file.
Indicate if the file being translated is a stub file. A stub file does not include the implementation of the methods. It has the suffix .pyi.
- property metavarsView on GitHub#
- property scopeView on GitHub#
The Scope object containing all objects defined within the current scope
- property semantic_doneView on GitHub#
- property syntax_doneView on GitHub#
- pyccel.parser.base.get_filename_from_import(module_name, input_folder_name, output_folder_name)[source]View on GitHub#
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)
stub 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. None if not found.
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. None if Python file is not found.