Last active
December 15, 2015 06:09
-
-
Save tomthorogood/5213995 to your computer and use it in GitHub Desktop.
A snippet from Mogu's MoguString(MultiString) class, which translates from Mogu's integral syntax to human-readable syntax
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def integral_to_script(self, original_integral): | |
| """ | |
| Converts integral commands to human-readable MoguScript. | |
| For instance, "2 58 foo 6 26 58 bar 6" would be translated to: | |
| 'set widget foo content to widget bar content' | |
| However, there can be contextual | |
| differences which may need to be set up, because some integral | |
| representations have multiple textual representations i | |
| (content|contents|text, for instance). | |
| If preferences don't exist, Mogu will use the "first' one it finds, | |
| but understand that because this information is stored in a dict, this | |
| is not guaranteed to be any specific keyword. | |
| setting up the preferences dict is easy, and avoids this problem: | |
| { | |
| syntax.as_integer['content'] : "content" | |
| } | |
| will use the word "content" whenever this ambiguity is encountered. | |
| """ | |
| # First, we must separate the string literals out of the equation, | |
| # since they will not be changed in any way. | |
| integral = self.separate_string_literals() | |
| # We can then safely explode the integral string, since | |
| # they are guaranteed by the script -> integral conversion | |
| # to be space-delimited, so long a there are no string literals. | |
| tokens = integral.split(' ') | |
| # In case there are mathematical expressions, we must first | |
| # resolve any syntax that may be confused with numbers. This | |
| # is done by reversing the string, finding tokens that are not | |
| # integers, and then finding the next integral token, which must | |
| # necessarily be a syntactical integral. | |
| tokens.reverse() | |
| searching = False | |
| for index,token in enumerate(tokens): | |
| if not MoguString.is_integer(token): | |
| searching = True | |
| elif searching is True: | |
| tokens[index] = self.getKeyword(token) | |
| searching = False | |
| tokens.reverse() # Put the tokens back in ltr order. | |
| # Next we need to do a search and replace for Mathematical | |
| # operators, since they will not be conflicting with any | |
| # integer literals. | |
| op_integrals = [self.getIntegral(op) for op in MoguString.Operators] | |
| for index,token in enumerate(tokens): | |
| if token in op_integrals: | |
| tokens[index] = self.getKeyword(token) | |
| # At this point, any mathematical expressions have been fully realised. | |
| # We can then replace any syntactical integers outside of parentheses. | |
| paren_level = 0 | |
| for index,token in enumerate(tokens): | |
| if token == '(': | |
| paren_level += 1 | |
| elif token == ')': | |
| paren_level -= 1 | |
| elif MoguString.is_integer(token): | |
| if paren_level == 0: | |
| tokens[index] = self.getKeyword(token) | |
| translated = " ".join(tokens) | |
| if self.string_literals: | |
| return translated % tuple(self.string_literals) | |
| else: | |
| return translated |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment