| Total Complexity | 4 |
| Total Lines | 34 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | import os |
||
| 8 | class LanguageDefinition(SectionCreatable): |
||
| 9 | |||
| 10 | def __init__(self, language: str, coalang_path=None): |
||
| 11 | """ |
||
| 12 | Creates a new LanguageDefinition object from file. |
||
| 13 | |||
| 14 | A Language Definition holds constants which may help parsing the |
||
| 15 | language. If you want to write a bear you'll probably want to use those |
||
| 16 | definitions to keep your bear independent of the semantics of each |
||
| 17 | language. |
||
| 18 | |||
| 19 | :param language: The actual language (e.g. C++). |
||
| 20 | :coalang_path: Path to coalang definition for language. |
||
| 21 | :raises FileNotFoundError: Raised when no definition is available for |
||
| 22 | the given family. |
||
| 23 | :raises KeyError: Raised when no definition is available for |
||
| 24 | the given language. |
||
| 25 | """ |
||
| 26 | SectionCreatable.__init__(self) |
||
| 27 | self.language = language.lower() |
||
| 28 | |||
| 29 | if not coalang_path: |
||
| 30 | filename = os.path.join(Constants.language_definitions, |
||
| 31 | language.lower() + ".coalang") |
||
| 32 | else: |
||
| 33 | filename = coalang_path |
||
| 34 | |||
| 35 | self.lang_dict = ConfParser().parse(filename)["default"] |
||
| 36 | |||
| 37 | def __getitem__(self, item): |
||
| 38 | return self.lang_dict[item] |
||
| 39 | |||
| 40 | def __contains__(self, item): |
||
| 41 | return item in self.lang_dict |
||
| 42 |