mib_generator.parsing package

These modules serve the purpose of parsing the inputted C/header files into a variety Python objects, that can be then easily accessed at the later construction stage of the process. The resulting product with parsed objects should contain all information in the input files, which requires the representation to be a bit convoluted from place to place, but still understandable I hope.

The submodules here are:

  • load - Module that allows for interaction between the parser and rest of the code.

  • parser_main - Module that initialises the parsing process for a given file and holds class that represents the result.

  • par_cfile - Module that holds the methods and classes for parsing .c files.

  • par_header - Module that holds the methods and classes for parsing header .h files.

  • par_header - Module holding various methods that are used by other modules throughout the parsing process.

Submodules

mib_generator.parsing.load module

Central location from which other modules can source data from the parsed C-files.

This module is initialised and its methods are called in the first step of the MIB construction process. At this point the C-files are loaded from the defined paths, are parsed using the mib_generator.parsing.parser_main.main method and are subsequently available for other scripts throughout the rest of the MIB construction process. The only other method here is extr_values which serves the purpose creating a global evaluation dictionary holding values from all header enums, macros, etc…

mib_generator.parsing.load.TmH

Holds the Python parsed representation of the Tm-header C-files. Each of type mib_generator.parsing.parser_main.file

Type:

list

mib_generator.parsing.load.TcH

Holds the Python parsed representation of the Tc-header C-files. Each of type mib_generator.parsing.parser_main.file

Type:

list

mib_generator.parsing.load.TcTmH

Holds the Python parsed representation of the TcTm-header C-files. Each of type mib_generator.parsing.parser_main.file

Type:

list

mib_generator.parsing.load.TmC

Holds the Python parsed representation of the Tm (normal) C-files. Each of type mib_generator.parsing.parser_main.file

Type:

list

mib_generator.parsing.load.enumerations

A dictionary containing all possible usable evaluations/enumerations sourced form enums, macros, etc… found in all 3 of the parsed header files.

Type:

dict

mib_generator.parsing.load.enum_stuff()[source]

Create a dictionary that includes all the possible evaluations from the input files.

For each of the inputted (and processed) C-files, this method extracts an evaluation (dictionary including all possible variable substitutions, from e.g. enum objects.) and then joins all of these dictionaries to one and assigns it to a global variable so it can be easily accessed.

mib_generator.parsing.load.extr_values(files)[source]

Create a dictionary from constants, enum correspondences, etc… in the given files.

Goes through all objects in the parsed files and for each enum and macro, appends the name-value pairs present to a dictionary (which represents the “global” evaluation in the file).

Parameters:

files (list) – Files from which the evaluation dictionary is to be extracted. Each of type mib_generator.parsing.parser_main.file

Returns:

A dictionary with all possible “global” evaluation found in the given file.

Return type:

dict

mib_generator.parsing.load.get_conf()[source]
mib_generator.parsing.load.get_paths()[source]

Load paths from the temporary config files.

This method looks up paths to each of the input files in the config files in the runtime config directory. It then saves these paths as global attributes of the mib_generator.parsing.load module, so they can be easily accessed to other methods in the whole program.

mib_generator.parsing.load.load_all()[source]

Run all initialisation and parsing methods.

This method (replacing previous simple initialisation of this module) runs other methods, which

  1. Load the paths to input C-files.

2. Load the configuration settings. 2. Parse files at these paths. 3. Create evaluation dictionary from these parsed files.

mib_generator.parsing.load.parse_all()[source]

Parse the inputted C-files and save the output.

This method uses the mib_generator.parsing.parser_main module to parse contents of the files specified at the paths given by global attributes of this module. It then assigns the outputs of the parsing process to various other global attributes of this module, so they can be easily accessed.

mib_generator.parsing.par_cfile module

Parsing and structural interpretation module for C files.

This module holds functions and classes serving the purpose of parsing C files containing information about the structure of packets and commands. The parsing in general happens mostly by recognising (a subset of) standard C-syntax and the system of Python representations of C-objects tries to include all possible information present in the C file, which makes it perhaps a bit too convoluted.

class mib_generator.parsing.par_cfile.instance(typ, inds, inde, cont)[source]

Bases: instance_og

Class of “first order” C structure/object found in the body of the file.

This class is a Python representation of an instance of a defined “top-level” object in the C-file. It does correspond to any specific type of such object (like struct, uint8, …) unlike its child-classes, but rather holds general properties and methods that all of these classes need, like parsing into array of elements.

All parameters are passed into the instance_og class from which this structure also inherits all its attributes.

name

The name of the instance of a C-object.

Type:

str

array

Equals to “-1” if the object isn’t an array and otherwise denotes the number of elements found in the array. Either as an int number or some abstract expression to be evaluated later.

Type:

str

elements

List of elements present if the structure is an array. Each of the elements is either of class struct_r or misc_r.

Type:

list

reposition()[source]

Use classic ordering if no specific position of each element of a structure is declared.

The C-code at the level of instances of the structure in the array can contain information about a custom ordering inside this array. This is found while parsing the structure-instances, however if this information is not present, the default ordering has to be used instead which is what this function does by rewriting positions of the elements in the array in such case.

str_parse(cont)[source]

Parse the content of the C-object into its syntactic/semantic components.

By first looking for whitespace characters and various types of brackets and then calling classes struct_r and misc_r, this function parses the text of the C-object into its name, array size and entries present.

Parameters:

cont (str) – The raw text of the C-object as found in the C file.

Returns:

A tuple containing:

  • str - The name of the C-structure.

  • str - See the attribute array above.

  • list - See the attribute elements above.

Return type:

tuple

class mib_generator.parsing.par_cfile.instance_og(typ, inds, inde, cont)[source]

Bases: object

Parent class of all representations of the C-objects.

This class holds properties which are shared by all Python representations of C-objects found in the analysed file.

Parameters:
  • typ (str) – The type of the C-object (e.g. struct or enum).

  • inds (int) – Starting index of the C-object.

  • inde (int) – End index of the C-object.

  • cont (str) – Raw text of the C-object as in the source file.

type

The type of the C-object (e.g. struct or enum).

Type:

str

start

Starting index of the C-object.

Type:

int

end

End index of the C-object.

Type:

int

text

Raw text of the C-object as in the source file.

Type:

str

comment

List holding all found interpretable comments associated to this C-object.

Type:

list

class mib_generator.parsing.par_cfile.misc_r(typ, inds, inde, cont)[source]

Bases: instance_og

Class of a singular instance of a constant within an array.

This class represents a specific instance of a constant (e.g. unit32, …) inside a defined array. The contents of this instance are parsed (based on whitespace characters and “=”) such that the extracted information are the position of this instance inside the array and the defined value of this instance.

All parameters are passed into the instance_og class from which this structure also inherits all its attributes.

position

Position of the instance inside an array. Can be either a specific integer or an abstract expression to be interpreted later.

Type:

str

value

A value of this instance of the constant. Can be either a specific integer or an abstract expression to be interpreted later.

Type:

str

mis_parse(cont)[source]

Parse the information about the constant instance.

Based on the presence of “=” and whitespace characters this function recognises the presence of an identifier of the position of the constant instance (inside the array) and interprets the rest as the expression/value assigned to this instance.

Parameters:

cont (str) – Raw text of the C-code defining the object instance.

Returns:

A tuple containing:

  • str - Position of the instance. See position.

  • str - A value of this instance of constant. See value.

Return type:

tuple

class mib_generator.parsing.par_cfile.miscal(typ, inds, inde, cont)[source]

Bases: instance

Class of an instance of an array of constants.

This class represents an instance of definition of a constant (e.g. unsigned integer, uint16, etc…) in the C-file. Being possibly an array of such objects, it inherits from instance, which already does most of the parsing, so at creation of this child-class, only some already parsed information are rewritten and corrected rather than the parsing happening from scratch.

All parameters are passed into the instance class from which this structure also inherits all its attributes.

name

Corrected (from instance) name of the object.

Type:

str

array

Corrected (from instance) information about the array of the objects.

Type:

str

add_parse(name)[source]

Correct information about the object’s name.

Looks again at the string identified by instance as the object’s name, correct it (e.g. delete whitespace characters from it) and also save this name as a property misc_r.flav of each instance of the object in the array.

Parameters:

name (str) – The possibly wrong name of the structure as recognised by the parser in instance

Returns:

The actual name of the structure.

Return type:

str

mib_generator.parsing.par_cfile.str_parse(stri)[source]

Parse the given string into blocks of “top-level” C-structures.

By going iteratively through the string and recognising the features of standard C-syntax, this function first marks the positions of all C-objects (including #define macro declaration) and then creates an appropriate Python representation-object of them, which then at its initialisation further parses the inner structure of these objects. After going through the whole string, this function then returns all found C-objects in a list.

Parameters:

stri (str) – A string to be parsed/analysed into the C-objects.

Returns:

List of Python representations of found C-objects, each represented by an object of child-class of instance

Return type:

list

class mib_generator.parsing.par_cfile.struct(typ, inds, inde, cont)[source]

Bases: instance

Class of an instance of an array of structs.

This class represents an instance of definition of a C-structure struct in the C-file. Being possibly an array of such structures, it inherits from instance, which already does most of the parsing, so at creation of this child-class only, some already parsed information are rewritten and corrected rather than the parsing happening from scratch.

All parameters are passed into the instance class from which this structure also inherits all its attributes.

flav

The type of the struct, i.e. name of the struct declared possibly in the header of which this structure/array of structures is an instance.

Type:

str

name

Corrected (from instance) name of the structure.

Type:

str

add_parse(name)[source]

Correct information about the object’s name and type.

Looks again at the string identified by instance as the object’s name and if it seems wrong (e.g. whitespace characters occur in it) further parse it and extract the actual name and flav of the structure from it.

Parameters:

name (str) – The possibly wrong name of the structure as recognised by the parser in instance

Returns:

A tuple containing:

  • str - The specific type of the structure. See flav.

  • str - The actual name of the structure.

Return type:

tuple

class mib_generator.parsing.par_cfile.struct_r(typ, inds, inde, cont)[source]

Bases: instance_og

Class of a singular instance of struct within an array.

This class represents a specific instance of a C-structure inside a defined array. The contents of this instance are parsed (based on brackets and commas) such that the extracted information are the position of this instance inside the array and the contents of the defined array represented by a Python dictionary.

All parameters are passed into the instance_og class from which this structure also inherits all its attributes.

position

Position of the instance inside an array. Can be either a specific integer or an abstract expression to be interpreted later.

Type:

str

entries

A dictionary representing the defined contents of the instance of the struct.

Type:

dict

srr_parse(cont)[source]

Parse the struct instance into its entries.

Based on the presence of “=”, brackets and commas, this function recognises the presence of an identifier of the position of the structure instance (inside the array) and each of the entries inside the structure definitions. It then further interprets the former and creates a dictionary from the latter.

Parameters:

cont (str) – Raw text of the C-code defining the object.

Returns:

A tuple containing:

  • str - Position of the instance. See position.

  • dict - A dictionary with the entries found in the struct instance.

Return type:

tuple

mib_generator.parsing.par_header module

Parsing and structural interpretation module for C-header files.

This module holds functions and classes serving the purpose of parsing C-header files containing information about the structure of packets and commands. The parsing in general happens mostly by recognising (a subset of) standard C-syntax and the system of Python representations of C-objects tries to include all possible information present in the C-header file, which makes it perhaps a bit too convoluted.

class mib_generator.parsing.par_header.define(typ, inds, inde, cont)[source]

Bases: structure

Class of a #define makro.

This class is a Python representation of an occurrence of #define macro in the C-header file. The inner structure of the macro is parsed and represented as a name and an expression.

All parameters are passed into the structure class from which this structure also inherits all its attributes.

name

The name of the definition in the macro.

Type:

str

expression

A string with the expression or value that the macro points to.

Type:

str

def_parse(cont)[source]

Parse the #define macro into its syntactic/semantic components.

By looking for whitespace characters, parses the text of the macro into its name and the expression it points to.

Parameters:

cont (str) – The text of the macro.

Returns:

A tuple containing:

  • str - The name of the macro.

  • str - The expression the macro points to.

Return type:

tuple

class mib_generator.parsing.par_header.enum(typ, inds, inde, cont)[source]

Bases: structure

Class of an enum C-object.

This class is a Python representation of an occurrence of enum in the C-header file. The inner structure of the enum is parsed and represented as a Python dictionary.

All parameters are passed into the structure class from which this structure also inherits all its attributes.

name

Name of the enum object.

Type:

str

entries

Dictionary containing key-value pairs found in the enum.

Type:

dict

ele_parse(cont)[source]

Parse enum into its syntactic/semantic components.

By recognising and parsing the inner syntax of enum this function identifies all elements in it and the values assigned to them (be it directly or implicitly by the order of the elements enum). Subsequently it represents these key-value pairs by a Python dictionary.

Parameters:

cont (str) – Text of the whole enum object.

Returns:

A tuple containing:

  • str - The name of the enum.

  • dict - A dictionary representing the entries in the enum and the values/expressions assigned to them.

Return type:

tuple

class mib_generator.parsing.par_header.enum_r(typ, inds, inde, cont)[source]

Bases: structure

Class of a reference to an enum inside a C-structure.

This class is used to represent occurrence of reference to enum inside a C-structure (a struct object). It can be of two types:

  1. It is only a reference to an enum defined elsewhere. In this case, the form attribute holds the name of this reference.

  2. There is a nested definition of the enum inside this C-structure. In this case, an object of class enum is used to represent the included enum and it is equated to form.

All parameters are passed into the structure class from which this structure also inherits all its attributes.

name

The name of the referenced enum.

Type:

str

form

Either name of the referenced enum or directly the object of the referenced enum.

Type:

str or enum

bites

Number of bites that the value of this enum is represented by.

Type:

int

array

An expression set to “-1” if the enum reference is not an array and to its number of elements (either integer value or an text expression to be later evaluated) in case it is an array.

Type:

str

enr_parse(cont)[source]

Parse the reference to its syntactic/semantic components.

By looking for “{” and “}” this function first decides whether the references includes a nested enum or only and external reference. Depending on the result it either calls the class enum or leaves the reference as a text. It then continues by looking for the name, array and bites information, etc by looking at the whitespaces and brackets.

Parameters:

cont (str) – The text of the enum reference to be analysed.

Returns:

A tuple consisting of:

  • str - The name of the referenced enum.

  • str or struct - See the attribute form above.

  • int - See the atribute bite above.

  • str - See the attribute array above.

Return type:

tuple

class mib_generator.parsing.par_header.extern(typ, inds, inde, cont)[source]

Bases: structure

Class of external declaration of a constant.

This class is a Python representation of an occurrence of external constant declaration in the C-header file. The inner structure of the expression is parsed and represented as its name, type (e.g. uint8) and an information on whether it is an array.

All parameters are passed into the structure class from which this structure also inherits all its attributes.

name

The name of the external declaration.

Type:

str

flav

The data type of the declaration (e.g. "uint8")

Type:

str

array

An expression set to “-1” if the declared constant is not an array and to its number of elements (either integer value or an text expression to be later evaluated) in case it is an array.

Type:

str

ext_parse(cont)[source]

Parse the external declaration of a constant into its syntactic/semantic components.

By looking for whitespace characters and square brackets, parses the text of the declaration into its name, data type and n information on whether it is an array.

Parameters:

cont (str) – The text of the declaration to be parsed.

Returns:

A tuple containing:

  • str - The data type of the declaration (e.g. "unsigned integer").

  • str - The name of the declaration.

  • str - The information on its array size (see above for more detail).

Return type:

tuple

class mib_generator.parsing.par_header.misc_r(typ, inds, inde, cont)[source]

Bases: structure

Class of a declaration of a constant inside a C-structure.

This class is a Python representation of an occurrence of constant value declaration (e.g. uint8) inside of some C-structure . The inner structure of the expression is parsed and represented as its name, bite length and an information on whether it is an array.

All parameters are passed into the structure class from which this structure also inherits all its attributes.

name

The name of the declared constant.

Type:

str

bites

Number of bites that the value of this constant is represented by.

Type:

int

array

An expression set to “-1” if the constant is not an array and to its number of elements (either integer value or an text expression to be later evaluated) in case it is an array.

Type:

str

mir_parse(cont)[source]

Parse the declaration into its syntactic/semantic components.

By looking at whitspaces and brackets in the string this function parses the text of the declaration into the name of the constant, and information about its bite-length and array-size.

Parameters:

cont (str) – The text of the constant declaration.

Returns:

A tuple containing:

  • str - The name of the constant.

  • int - The declared bite-length of the constant.

  • str - See the attribute array above.

Return type:

tuple

mib_generator.parsing.par_header.str_parse(stri)[source]

Parse the given string into blocks of “top-level” C-structures.

By going iteratively through the string and recognising the features of standard C-syntax, this function first marks the positions of all C-objects (including #define macro declaration) and then creates an appropriate Python representation-object of them, which then at its initialisation further parses the inner structure of these objects. After going through the whole string, this function then returns all found C-objects in a list.

Parameters:

stri (str) – A string to be parsed/analysed into the C-objects.

Returns:

List of Python representations of found C-objects, each represented by an object of child-class of structure

Return type:

list

mib_generator.parsing.par_header.str_parse_r(offset, stri)[source]

Parse the given string into “2nd-order” blocks of C-structures.

Unlike for str_parse this time “second-order” strings (that is strings inside other already recognised C-objects; i.e. e.g. inner content of struct declaration) are expected and the parsing itslef is recursive (in order to account e.g. for multiple nested occurences of struct).

Parameters:
  • offset (int) – Offset of this string from the start of the original file. I.e. original start index of its first character.

  • stri (str) – The string to be parsed/interpreted into its contents.

Returns:

List of identified and interpreted C-object inside the original string. Each is represented by an object which is some child-class of the class structure.

Return type:

list

class mib_generator.parsing.par_header.struct(typ, inds, inde, cont)[source]

Bases: structure

Class of declaration of a C-structure struct ....

This class is a Python representation of an occurrence of struct structure in the C-header file. The inner structure of this … structure is represented by its name, information on whether it is packed and its contents/elements (which are themselves other similar Python objects).

All parameters are passed into the structure class from which this structure also inherits all its attributes.

name

The name of the structure.

Type:

str

packed

True if the structure is packed, false otherwise.

Type:

bool

elements

A list of elements inside this structure. Each is represented by a Python object which is some child-class of structure

Type:

list

stc_parse(cont)[source]

Parse the struct C-structure into its syntactic/semantic components.

By first looking for whitespace characters and “{” and then calling str_parse_r, this function parses the text of the struct object into its name, entries, etc.

Parameters:

cont (str) – The raw text of the struct C-structure in the C-header file.

Returns:

A tuple containing:

  • str - The name of the C-structure.

  • bool - True if the structure is packed, False otherwise.

  • list - List of elements inside the structure. Each represented by an object of child-class of structure.

Return type:

tuple

class mib_generator.parsing.par_header.struct_r(typ, inds, inde, cont)[source]

Bases: structure

Class of a reference to a C-structure struct inside another.

This class is used to represent occurrence of declaration of one structure inside another. It can be of two types:

  1. It is only a reference to a structure defined elsewhere. In this case, the form attribute holds the name of this reference.

  2. There is a nested definition of one structure inside another. In this case, an object of class struct is used to represent the included structure and it is equated to form.

All parameters are passed into the structure class from which this structure also inherits all its attributes.

name

The name of the referenced C-structure.

Type:

str

form

Either name of the referenced structure or directly the object of the referenced structure.

Type:

str or struct

array

An expression set to “-1” if the struct reference is not an array and to its number of elements (either integer value or an text expression to be later evaluated) in case it is an array.

Type:

str

str_parse(cont)[source]

Parse the reference to its syntactic/semantic components.

By looking for “{” and “}” this function first decides whether the references includes a nested structure or only and external reference. Depending on the result it either calls the class struct or leaves the reference as a text. It then continues by looking for the name, array information, etc by looking at the whitespaces and brackets.

Parameters:

cont (str) – The text of the structure reference to be analysed.

Returns:

A tuple consisting of:

  • str - The name of the referenced structure.

  • str or struct - See the attribute form above.

  • str - See the attribute array above.

Return type:

tuple

class mib_generator.parsing.par_header.structure(typ, inds, inde, cont)[source]

Bases: object

Parent class of all representations of the C-objects.

This class holds properties which are shared by all Python representations of C-objects found in the analysed file.

Parameters:
  • typ (str) – The type of the C-object (e.g. struct or enum).

  • inds (int) – Starting index of the C-object.

  • inde (int) – End index of the C-object.

  • cont (str) – Raw text of the C-object as in the source file.

type

The type of the C-object (e.g. struct or enum).

Type:

str

start

Starting index of the C-object.

Type:

int

end

End index of the C-object.

Type:

int

text

Raw text of the C-object as in the source file.

Type:

str

comment

List holding all found interpretable comments associated to this C-object.

Type:

list

mib_generator.parsing.par_methods module

Depository of parsing methods.

This module holds various methods used for parsing of the header and normal C files and their subsequent interpretation into corresponding python objects.

mib_generator.parsing.par_methods.clean(stri, tokens)[source]

Replace tokens with spaces.

Takes a string and a list of tokens and replaces all occurences of the tokens in the string with an appropriate number of space so the absolute positions of unaffected parts of the string stay the same.

Parameters:
  • stri (str) – A string to be “cleaned”.

  • tokens (set) – A set of tokens to be replaced.

Returns:

The resulting “cleaned” string.

Return type:

str

mib_generator.parsing.par_methods.com_parse(comm)[source]

Parse the comments in the string into blocks corresponding to singular comments.

Looks at each passed comment and evaluates whether it should be interpreted or not. If yes, it then calls the class comment from it and adds this object to the list of interpreted comments.

Parameters:

comm (list) – List of strings each representing the content of a single comment.

Returns:

List of found interpreted comments, each represented by an object of the comment class.

Return type:

list

class mib_generator.parsing.par_methods.comment(start, end, text)[source]

Bases: object

Class representing an occurrence of an interpretable comment in the file, entries found in it, etc.

Saves the comment text and its start/end indexes and then it tries to interpret the comment as a dictionary represented in the json5 format.

Parameters:
  • start (int) – Start index of the comment in the original file.

  • end (int) – End index of the comment in the original file.

  • text (str) – Original text of the comment.

start

Start index of the comment in the original file.

Type:

int

end

End index of the comment in the original file.

Type:

int

text

Original text of the comment.

Type:

str

entries

Dictionary containing the content of the comment found in the json5 string.

Type:

dict

mib_generator.parsing.par_methods.erase_text(stri)[source]

Erase all text inside quotation marks in the given string.

Looks for first order quotation marks (that is, not inside another quotation marks) and replaces their content with matching amount of spaces so that the total length of the string remains the same.

Parameters:

stri (str) – The string in which the quotation marks are to be replaced.

Returns:

The string with the quotation marks contents replaced.

Return type:

str

mib_generator.parsing.par_methods.line_index(stri)[source]

Give list of indexes of line starts of the given string.

Based on the newline character, looks for indexes of new lines and adds them to a list.

Parameters:

stri (str) – The string to be analysed.

Returns:

List of indexes at which new lines start.

Return type:

list

mib_generator.parsing.par_methods.preproc_eval(variab)[source]

Evaluate whether a pre-processing condition holds.

Looks into config file whether the passed pre-processing condition holds. If the condition name does not occur in the config file, asks the user what boolean value it should assign to it.

Parameters:

variab (str) – Name of the pre-processing variable/condition to be evaluated.

Returns:

Truth-value of the condition.

Return type:

bool

mib_generator.parsing.par_methods.preproc_filter(stri_o, stri_c)[source]

Filter out what parts of the C-code should be further considered based on preprocessor directives.

First, this function looks for blocks pre-processor logic using the preproc_parse function. It then evaluates the condition associated with this logic with preproc_eval and depending on the result, decides what parts of the string should be deleted (replaced by equal number of spaces). Finally it performs this replacement.

The preprocessor logic is determined based on the passed string with the code (stri_o), but the deliting is applied also to the string with comment (stri_c).

Parameters:
  • stri_o (str) – The text with C-code to be filtered on the basis of pre-processor logic.

  • stri_c (str) – The text with comments to be filtered on the basis of pre-processor logic.

Returns:

A tuple containing:

  • str - The string with C-code with the pre-processor logic applied.

  • str - The string with comments with the pre-processor logic applied.

Return type:

tuple

mib_generator.parsing.par_methods.preproc_parse(stri)[source]

Parse C-preprocessor conditional syntax into logic blocks.

Looks for logical C-preprocessor directives (specifically #ifdef, #ifndef, etc.), and marks the start/end indices of the corresponding “logic blocks” (i.e. sections beween #ifdef and #else). Then returns these block indexes in a list. Also performs some checks along the way.

Parameters:

stri (str) – The string to be analysed/parsed.

Returns:

List of list of indexes denoting start/end positions of the logic blocks.

Return type:

list

mib_generator.parsing.par_methods.split_comment(stri)[source]

Split the given string into a section with C comments and a section without them.

Goes iteratively through the string and based on single/multi-line C-comment syntax, recognised blocks of comments and marks their start/end positions. It then constructs versions of the passed string with the comment/code blocks ommited.

Parameters:

stri (str) – The string to be split into comments/code.

Returns:

A tuple consisting of:
  • str - The original string with comments omitted.

  • str - The original string with C-code omitted.

Return type:

tuple

mib_generator.parsing.parser_main module

The main parsing module.

This module is a starting point for parsing of C-files and holds class which plays the role of their interpretation.

class mib_generator.parsing.parser_main.file(stri, header)[source]

Bases: object

Class representing the file being analysed and its internal structure in an easily Python-accessible format.

During creation this class goes through multiple steps of parsing/interpretation using methods in mib_generator.parsing.par_header, mib_generator.parsing.par_cfile and parsing.par_methods. The steps are as follows:

  1. Get general information about the file (length, line indexes)

  2. Separate text in comment and the code (while keeping the absolute positions in the file).

  3. Process any C-preprocessor logic that’s in the code.

  4. Parse the resulting code into Python representations of the corresponding C-objects.

  5. Parse the comments into intepretable sections (written in json5) and interpret their content.

  6. Create links between the found C-objects and interterpretable comments

Parameters:
  • stri (str) – The content of the file to be parsed/analysed.

  • header (bool) – Parameter denoting whether file is a header or normal C file.

text

Text of the file.

Type:

str

max_position

Length of the file.

Type:

int

lines

List of positions of line starts.

Type:

list

text_o

File text with only code (without comments).

Type:

str

text_c

File text with only comments (without code).

Type:

str

text_of

File (with code) after sorting out the pre-processor logic.

Type:

str

text_cf

File (with comments) after sorting out the pre-processor logic.

Type:

str

structures

List of Python representations of C-objects found in the file.

The Python representations are child classes of either mib_generator.parsing.par_cfile.instance_og or mib_generator.parsing.par_header.structure

Type:

list

comments

List of found interpretable comments which are represented using mib_generator.parsing.par_methods.comment

Type:

list

List of pairs of comments and the Python representation of C-objects they belong to.

Type:

list

linker()[source]

Link each comment to a corresponding C-object in the file

Goes one by one through comments and searches for their corresponding structure. For the closes found match, creates link from the comment to the structure and from the structure to the comment (by generating attributes for their respectives classes) and returns them in a list of pairs.

Returns:

Pairs of comments and their corresponding structures.

Return type:

list of pairs

mib_generator.parsing.parser_main.main(name='/home/vachaj11/Documents/MIB/start/src/PUS_TmDefs.h')[source]

Create a parsed python representation of a file.

Opens file, depending on its ending chooses correct parser and lets it run.

Parameters:

name (str) – Path to file.

Returns:

File object

Return type:

file