| Total Complexity | 18 |
| Total Lines | 113 |
| Duplicated Lines | 0 % |
| 1 | import os |
||
| 4 | from coalib.misc.Constants import Constants |
||
| 5 | from coalib.parsing.ConfParser import ConfParser |
||
| 6 | |||
| 7 | |||
| 8 | class LanguageDefinition(SectionCreatable): |
||
| 9 | def __init__(self, language_family: str, language: str): |
||
| 10 | """ |
||
| 11 | Creates a new LanguageDefinition object from file. |
||
| 12 | |||
| 13 | A Language Definition holds constants which may help parsing the |
||
| 14 | language. If you want to write a bear you'll probably want to use those |
||
| 15 | definitions to keep your bear independent of the semantics of each |
||
| 16 | language. |
||
| 17 | |||
| 18 | :param language_family: The language family. E.g. C for C++ and C |
||
| 19 | and C# and so on. |
||
| 20 | :param language: The actual language (e.g. C++). |
||
| 21 | :raises FileNotFoundError: Raised when no definition is available for |
||
| 22 | the given family. This is a compatability |
||
| 23 | exception from module |
||
| 24 | `coalib.misc.Compatability`. |
||
| 25 | :raises KeyError: Raised when no definition is available for |
||
| 26 | the given language. |
||
| 27 | """ |
||
| 28 | SectionCreatable.__init__(self) |
||
| 29 | self.language = language.lower() |
||
| 30 | filename = os.path.join(Constants.language_definitions, |
||
| 31 | language_family.lower() + ".coalang") |
||
| 32 | self.lang_dict = ConfParser().parse(filename)[language.lower()] |
||
| 33 | |||
| 34 | def __getitem__(self, item): |
||
| 35 | return self.lang_dict[item] |
||
| 36 |