private TexFormula Parse(string value, ref int position, bool allowClosingDelimiter, string textStyle) { var formula = new TexFormula() { TextStyle = textStyle }; var closedDelimiter = false; while (position < value.Length && !(allowClosingDelimiter && closedDelimiter)) { char ch = value[position]; if (IsWhiteSpace(ch)) { position++; } else if (ch == escapeChar) { ProcessEscapeSequence(formula, value, ref position, allowClosingDelimiter, ref closedDelimiter); } else if (ch == leftGroupChar) { var groupValue = ReadGroup(formula, value, ref position, leftGroupChar, rightGroupChar); var parsedGroup = Parse(groupValue, textStyle); var innerGroupAtom = parsedGroup.RootAtom ?? new RowAtom(); var groupAtom = new TypedAtom(innerGroupAtom, TexAtomType.Ordinary, TexAtomType.Ordinary); formula.Add(AttachScripts(formula, value, ref position, groupAtom)); } else if (ch == rightGroupChar) { throw new TexParseException("Found a closing '" + rightGroupChar + "' without an opening '" + leftGroupChar + "'!"); } else if (ch == superScriptChar || ch == subScriptChar || ch == primeChar) { if (position == 0) { throw new TexParseException("Every script needs a base: \"" + superScriptChar + "\", \"" + subScriptChar + "\" and \"" + primeChar + "\" can't be the first character!"); } else { throw new TexParseException("Double scripts found! Try using more braces."); } } else { var scriptsAtom = AttachScripts(formula, value, ref position, ConvertCharacter(formula, value, ref position, ch)); formula.Add(scriptsAtom); } } return(formula); }
private TexFormula Parse( SourceSpan value, ref int position, bool allowClosingDelimiter, string textStyle, ICommandEnvironment environment) { var formula = new TexFormula { Source = value, TextStyle = textStyle }; var closedDelimiter = false; var skipWhiteSpace = ShouldSkipWhiteSpace(textStyle); var initialPosition = position; while (position < value.Length && !(allowClosingDelimiter && closedDelimiter)) { char ch = value[position]; var source = value.Segment(position, 1); if (IsWhiteSpace(ch)) { if (!skipWhiteSpace) { formula.Add(new SpaceAtom(source), source); } position++; } else if (ch == escapeChar) { ProcessEscapeSequence( formula, value, ref position, allowClosingDelimiter, ref closedDelimiter, environment); } else if (ch == leftGroupChar) { var groupValue = ReadElement(value, ref position); var parsedGroup = Parse(groupValue, textStyle, environment.CreateChildEnvironment()); var innerGroupAtom = parsedGroup.RootAtom ?? new RowAtom(groupValue); var groupAtom = new TypedAtom( innerGroupAtom.Source, innerGroupAtom, TexAtomType.Ordinary, TexAtomType.Ordinary); var scriptsAtom = this.AttachScripts(formula, value, ref position, groupAtom, true, environment); formula.Add(scriptsAtom, value.Segment(initialPosition, position - initialPosition)); } else if (ch == rightGroupChar) { throw new TexParseException("Found a closing '" + rightGroupChar + "' without an opening '" + leftGroupChar + "'!"); } else if (ch == superScriptChar || ch == subScriptChar || ch == primeChar) { if (position == 0) { throw new TexParseException("Every script needs a base: \"" + superScriptChar + "\", \"" + subScriptChar + "\" and \"" + primeChar + "\" can't be the first character!"); } else { throw new TexParseException("Double scripts found! Try using more braces."); } } else { var character = ConvertCharacter(formula, ref position, source, environment); if (character != null) { var scriptsAtom = AttachScripts( formula, value, ref position, character, skipWhiteSpace, environment); formula.Add(scriptsAtom, value.Segment(initialPosition, position)); } } } return(formula); }