Advertisement
Guest User

coloring.py

a guest
Mar 20th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 13.44 KB | None | 0 0
  1. __author__ = "Nemanja Starčev"
  2. __copyright__ = "textX-tools"
  3. __license__ = "MIT"
  4.  
  5. class ColoringVSCode(object):
  6.  
  7.     def __init__(self, config):
  8.         self.config = config
  9.         self.textxRulesInProgram = {}
  10.         self.keywords = []
  11.         self.operations = []
  12.         self.rule_keyword_relation = []
  13.         self.rule_operation_relation = []
  14.         self.special_characters = ['+','*','?','|','.','(',')','$','[',']','\\','^']
  15.         self.additional_characters = ['<', '>', '=', '+', '-', '*', '/', '|', '&', '(', ')', '~', '!', '@', '#', '$', '[', ']', '{', '}', ',', '.', ';', ':', '?', '%', '\\', '^']
  16.         self.default_keyword_type = None
  17.         self.default_operation_type = None
  18.         self.line_comment = None
  19.         self.block_comment_start = None
  20.         self.block_comment_end = None
  21.         self.rules_keyword_type_relation = {}
  22.         self.rules_operation_type_relation = {}
  23.         self.matches_word_type_relation = {}
  24.         self.regular_expressions = {}
  25.  
  26.         self.keyword_type_relation = {}
  27.         self.operation_type_relation = {}
  28.         self.regular_expression_type_relation = {}
  29.         self.type_keyword_relation = {}
  30.         self.type_operation_relation = {}
  31.         self.type_regular_expression_relation = {}
  32.  
  33.         self.coloring_model = {}
  34.  
  35.     def get_coloring_model(self):
  36.         try:
  37.             self._interpret_grammar(self.config.grammar_model)
  38.             self._interpret_program(self.config.coloring_model)
  39.             self._prepare_data()
  40.             return self.coloring_model
  41.         except:
  42.             pass
  43.  
  44.     def _interpret_program(self,model):
  45.         if model.configuration != None:
  46.             self._interpret_configuration(model.configuration)
  47.         for element in model.array:
  48.             if element.rules != None:
  49.                 self._intepret_rules(element.rules)
  50.             if element.matches != None:
  51.                 self._interpret_matches(element.matches)
  52.             if element.regular_expressions != None:
  53.                 self._interpret_regular_expressions(element.regular_expressions)
  54.  
  55.     def _interpret_configuration(self,configuration):
  56.         for command in configuration.configuration_commands:
  57.             if command.default != None:
  58.                 self._interpret_default(command.default)
  59.             if command.coment != None:
  60.                 self._interpret_comment(command.coment)
  61.  
  62.     def _interpret_default(self,default):
  63.         for option in default.default_options:
  64.             if option.default_keyword_option != None:
  65.                 self.default_keyword_type = option.default_keyword_option.type
  66.             if option.default_operation != None:
  67.                 self.default_operation_type = option.default_operation.type
  68.  
  69.     def _interpret_comment(self,comment):
  70.         for option in comment.comment_options:
  71.             if option.line != None:
  72.                 self.line_comment = self._add_slash_infront_of_special_characters(option.line.id)
  73.             if option.block != None:
  74.                 self.block_comment_start = self._add_slash_infront_of_special_characters(option.block.start)
  75.                 self.block_comment_end = self._add_slash_infront_of_special_characters(option.block.end)
  76.  
  77.     def _intepret_rules(self,rules):
  78.         for option in rules.rule_options:
  79.             if option.rule_keyword != None:
  80.                 for list in option.rule_keyword.rule_lists:
  81.                     for element in list.elements:
  82.                         self.rules_keyword_type_relation[element] = list.type
  83.             if option.rule_operation != None:
  84.                 for list in option.rule_operation.rule_lists:
  85.                     for element in list.elements:
  86.                         self.rules_operation_type_relation[element] = list.type
  87.  
  88.     def _interpret_matches(self, matches):
  89.         for list in matches.match_list:
  90.             for word in list.words:
  91.                 self.matches_word_type_relation[self._add_slash_infront_of_special_characters(word)] = list.type
  92.  
  93.     def _interpret_regular_expressions(self,regular_expressions):
  94.         for list in regular_expressions.regular_expression_list:
  95.             for expression in list.expression:
  96.                 self.regular_expressions[expression] = list.type
  97.  
  98.     def _interpret_grammar(self, model):
  99.         for rule in model.rules:
  100.             self._interpret_sequences(rule.body.sequences,rule.name)
  101.  
  102.     def _interpret_sequences(self,sequences,rule_name):
  103.         for sequence in sequences:
  104.             for expression in sequence.repeatable_expr:
  105.                 if expression.expr.simple_match != None and expression.expr.simple_match.str_match != None:
  106.                     self._append_word(rule_name, expression.expr.simple_match.str_match.match)
  107.                 if expression.expr.assigment != None and expression.expr.assigment.rhs != None:
  108.                     if expression.expr.assigment.rhs.simple != None and expression.expr.assigment.rhs.simple.str_match != None:
  109.                         self._append_word(rule_name, expression.expr.assigment.rhs.simple.str_match.match)
  110.                     if expression.expr.assigment.rhs.modifiers != None \
  111.                         and expression.expr.assigment.rhs.modifiers.str_match != None:
  112.                         self._append_word(rule_name, expression.expr.assigment.rhs.modifiers.str_match.match)
  113.                 if expression.operator != None and expression.operator.modifiers != None \
  114.                         and expression.operator.modifiers.str_match != None:
  115.                     self._append_word(rule_name, expression.operator.modifiers.str_match.match)
  116.                 if expression.expr.bracketed_choice != None:
  117.                     self._interpret_sequences(expression.expr.bracketed_choice.choice.sequences, rule_name)
  118.  
  119.     def _append_word(self, rule_name, word):
  120.         if self._is_word_assembled_from_additional_characters(word):
  121.             word = self._add_slash_infront_of_special_characters(word)
  122.             if word not in self.operations:
  123.                 self.operations.append(word)
  124.             operation = {'rule': rule_name, 'operation': word}
  125.             self.rule_operation_relation.append(operation)
  126.         else:
  127.             word = self._add_slash_infront_of_special_characters(word)
  128.             if word not in self.keywords:
  129.                 self.keywords.append(word)
  130.             keyword = {'rule': rule_name, 'keyword': word}
  131.             self.rule_keyword_relation.append(keyword)
  132.  
  133.     def _is_word_assembled_from_additional_characters(self,word):
  134.         for character in word:
  135.             if character not in self.additional_characters:
  136.                 return False
  137.         return True
  138.  
  139.     def _add_slash_infront_of_special_characters(self, word):
  140.         retVal = ""
  141.         for  i, character in enumerate(word):
  142.             if character not in self.special_characters:
  143.                 retVal += character
  144.             else:
  145.                 retVal += '\\\\'
  146.                 retVal += character
  147.         return retVal
  148.  
  149.     def _prepare_data(self):
  150.         self._prepare_relation_keywords()
  151.         self._prepare_relation_operations()
  152.         self._prepare_types()
  153.         self._prepare_coloring_json()
  154.  
  155.     def _prepare_relation_keywords(self):
  156.         for item in self.matches_word_type_relation:
  157.             if item not in self.keywords and self._is_word_assembled_from_additional_characters(item) == False:
  158.                 self.keywords.append(item)
  159.         for item in self.keywords:
  160.             self.type = self._get_type_from_keywords(item)
  161.             if item not in self.keyword_type_relation and self.type != None:
  162.                 self.keyword_type_relation[item] = self.type
  163.                 continue
  164.             self.type = self._get_type_from_rules_keyword(item)
  165.             if self.type != None:
  166.                 self.keyword_type_relation[item] = self.type
  167.                 continue
  168.             if self.default_keyword_type != None:
  169.                 self.keyword_type_relation[item] = self.default_keyword_type
  170.  
  171.     def _prepare_relation_operations(self):
  172.         for item in self.matches_word_type_relation:
  173.             if item not in self.operations and self._is_word_assembled_from_additional_characters(item):
  174.                 self.operations.append(item)
  175.         for item in self.operations:
  176.             self.type = self._get_type_from_keywords(item)
  177.             if item not in self.operation_type_relation and self.type != None:
  178.                 self.operation_type_relation[item] = self.type
  179.                 continue
  180.             self.type = self._get_type_from_rules_operation(item)
  181.             if self.type != None:
  182.                 self.operation_type_relation[item] = self.type
  183.                 continue
  184.             if self.default_operation_type != None:
  185.                 self.operation_type_relation[item] = self.default_operation_type
  186.  
  187.  
  188.     def _get_type_from_keywords(self, word):
  189.         self.type = None
  190.         for item in self.matches_word_type_relation:
  191.             if item == word:
  192.                 self.type = self.matches_word_type_relation.get(item)
  193.         if self.type != None:
  194.             return self.type
  195.         for item in self.keyword_type_relation:
  196.             if item ==  word:
  197.                 self.type = self.keyword_type_relation.get(item)
  198.         return self.type
  199.  
  200.     def _get_type_from_rules_keyword(self, word):
  201.         self.type = None
  202.         for item in self.rule_keyword_relation:
  203.             if item['keyword'] == word:
  204.                 for relation in self.rules_keyword_type_relation:
  205.                     if relation == item['rule']:
  206.                         self.type = self.rules_keyword_type_relation[relation]
  207.         return self.type
  208.  
  209.     def _get_type_from_rules_operation(self, word):
  210.         self.type = None
  211.         for item in self.rule_operation_relation:
  212.             if item['operation'] == word:
  213.                 for relation in self.rules_operation_type_relation:
  214.                     if relation == item['rule']:
  215.                         self.type = self.rules_operation_type_relation[relation]
  216.         return self.type
  217.  
  218.     def _prepare_types(self):
  219.         for item in self.keyword_type_relation:
  220.             if self.keyword_type_relation[item] not in self.type_keyword_relation:
  221.                 self.type_keyword_relation[self.keyword_type_relation[item]] = []
  222.             if item != "'" and item != '"' and self._is_word_assembled_from_additional_characters(item) == False:
  223.                 self.type_keyword_relation[self.keyword_type_relation[item]].append(item)
  224.             elif item != "'" and item != '"':
  225.                 self.type_keyword_relation[self.keyword_type_relation[item]].append(item)
  226.         for item in self.operation_type_relation:
  227.             if self.operation_type_relation[item] not in self.type_operation_relation:
  228.                 self.type_operation_relation[self.operation_type_relation[item]] = []
  229.             self.type_operation_relation[self.operation_type_relation[item]].append(item)
  230.         for item in self.regular_expressions:
  231.             if self.regular_expressions[item] not in self.type_regular_expression_relation:
  232.                 self.type_regular_expression_relation[self.regular_expressions[item]] = []
  233.             self.type_regular_expression_relation[self.regular_expressions[item]].append(item)
  234.             self.type_regular_expression_relation[self.regular_expressions[item]].append(item)
  235.  
  236.     def _prepare_coloring_json(self):
  237.         self.coloring_model = {
  238.             'extensions': self.config.config.lang_ext_double_quoted,
  239.             'comment': {
  240.                 'line': self.line_comment,
  241.                 'block_start': self.block_comment_start,
  242.                 'block_end': self.block_comment_end
  243.             },
  244.             'keywords': self._get_name_match_relation(self.type_keyword_relation, True),
  245.             'operations': self._get_name_match_relation(self.type_operation_relation),
  246.             'regular_expressions':  self._get_name_match_relation(self.type_regular_expression_relation)
  247.         }
  248.  
  249.     def _get_name_match_relation(self, map, word_boundary=False):
  250.         keywords = []
  251.         deleted = True
  252.         while deleted == True:
  253.             deleted = False
  254.             for item, words in map.items():
  255.                 element = ""
  256.                 j = 0
  257.                 insert = False
  258.                 while j < len(words):
  259.                     if j >= len(words):
  260.                         break
  261.                     indipendent = self._isWordIndipendent(words[j], item, map)
  262.                     if indipendent == False:
  263.                         j = j + 1
  264.                         continue
  265.                     string = ""
  266.                     if word_boundary:
  267.                         string += "\\\\b"
  268.                     string += words[j]
  269.                     if word_boundary:
  270.                         string += "\\\\b"
  271.                     element += string
  272.                     if j != len(words)-1:
  273.                         element += '|'
  274.                     words.remove(words[j])
  275.                     deleted = True
  276.                     insert = True
  277.                 keyword = {
  278.                     'name': item,
  279.                     'match': element
  280.                 }
  281.                 if insert:
  282.                     keywords.append(keyword)
  283.         return keywords
  284.  
  285.     def _isWordIndipendent(self, word, type, map):
  286.         for key, value in map.items():
  287.             for item in value:
  288.                 if key != type and word in item:
  289.                     return False
  290.         return True
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement