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); }
private void ProcessEscapeSequence(TexFormula formula, SourceSpan value, ref int position, bool allowClosingDelimiter, ref bool closedDelimiter, ICommandEnvironment environment) { var initialSrcPosition = position; position++; var start = position; while (position < value.Length) { var ch = value[position]; var isEnd = position == value.Length - 1; if (!char.IsLetter(ch) || isEnd) { // Escape sequence has ended // Or it's a symbol. Assuming in this case it will only be a single char. if ((isEnd && char.IsLetter(ch)) || position - start == 0) { position++; } break; } position++; } var commandSpan = value.Segment(start, position - start); var command = commandSpan.ToString(); var formulaSource = new SourceSpan(value.Source, initialSrcPosition, commandSpan.End); SymbolAtom symbolAtom = null; if (SymbolAtom.TryGetAtom(commandSpan, out symbolAtom)) { // Symbol was found. if (symbolAtom.Type == TexAtomType.Accent) { var helper = new TexFormulaHelper(formula, formulaSource); TexFormula accentFormula = ReadScript(formula, value, ref position, environment); helper.AddAccent(accentFormula, symbolAtom.Name); } else if (symbolAtom.Type == TexAtomType.BigOperator) { var opAtom = new BigOperatorAtom(formulaSource, symbolAtom, null, null); formula.Add(AttachScripts(formula, value, ref position, opAtom, true, environment), formulaSource); } else { formula.Add( AttachScripts(formula, value, ref position, symbolAtom, true, environment), formulaSource); } } else if (predefinedFormulas.TryGetValue(command, out var factory)) { // Predefined formula was found. var predefinedFormula = factory(formulaSource); var atom = AttachScripts(formula, value, ref position, predefinedFormula.RootAtom, true, environment); formula.Add(atom, formulaSource); } else if (command.Equals("nbsp")) { // Space was found. var atom = AttachScripts(formula, value, ref position, new SpaceAtom(formulaSource), true, environment); formula.Add(atom, formulaSource); } else if (textStyles.Contains(command)) { // Text style was found. SkipWhiteSpace(value, ref position); var styledFormula = command == TexUtilities.TextStyleName ? ConvertRawText(ReadElement(value, ref position), command) : Parse(ReadElement(value, ref position), command, environment.CreateChildEnvironment()); if (styledFormula.RootAtom == null) { throw new TexParseException("Styled text can't be empty!"); } var atom = AttachScripts(formula, value, ref position, styledFormula.RootAtom, true, environment); var source = new SourceSpan(formulaSource.Source, formulaSource.Start, position); formula.Add(atom, source); } else if (embeddedCommands.Contains(command) || environment.AvailableCommands.ContainsKey(command) || _commandRegistry.ContainsKey(command)) { // Command was found. var commandAtom = ProcessCommand( formula, value, ref position, command, allowClosingDelimiter, ref closedDelimiter, environment); if (commandAtom != null) { commandAtom = allowClosingDelimiter ? commandAtom : AttachScripts( formula, value, ref position, commandAtom, true, environment); var source = new SourceSpan(formulaSource.Source, formulaSource.Start, commandAtom.Source.End); formula.Add(commandAtom, source); } } else { // Escape sequence is invalid. throw new TexParseException("Unknown symbol or command or predefined TeXFormula: '" + command + "'"); } }
private TexFormula ReadScript( TexFormula formula, SourceSpan value, ref int position, ICommandEnvironment environment) => Parse(ReadElement(value, ref position), formula.TextStyle, environment.CreateChildEnvironment());
/// <remarks>May return <c>null</c> for commands that produce no atoms.</remarks> private Atom ProcessCommand( TexFormula formula, SourceSpan value, ref int position, string command, bool allowClosingDelimiter, ref bool closedDelimiter, ICommandEnvironment environment) { int start = position - command.Length; SourceSpan source; switch (command) { case "frac": { var numeratorFormula = Parse( ReadElement(value, ref position), formula.TextStyle, environment.CreateChildEnvironment()); var denominatorFormula = Parse( ReadElement(value, ref position), formula.TextStyle, environment.CreateChildEnvironment()); source = value.Segment(start, position - start); return(new FractionAtom(source, numeratorFormula.RootAtom, denominatorFormula.RootAtom, true)); } case "left": { SkipWhiteSpace(value, ref position); if (position == value.Length) { throw new TexParseException("`left` command should be passed a delimiter"); } var delimiter = value[position]; ++position; var left = position; var internals = ParseUntilDelimiter(value, ref position, formula.TextStyle, environment); var opening = GetDelimiterSymbol( GetDelimeterMapping(delimiter), value.Segment(start, left - start)); if (opening == null) { throw new TexParseException($"Cannot find delimiter named {delimiter}"); } var closing = internals.ClosingDelimiter; source = value.Segment(start, position - start); return(new FencedAtom(source, internals.Body, opening, closing)); } case "overline": { var overlineFormula = Parse( ReadElement(value, ref position), formula.TextStyle, environment.CreateChildEnvironment()); source = value.Segment(start, position - start); return(new OverlinedAtom(source, overlineFormula.RootAtom)); } case "right": { if (!allowClosingDelimiter) { throw new TexParseException("`right` command is not allowed without `left`"); } SkipWhiteSpace(value, ref position); if (position == value.Length) { throw new TexParseException("`right` command should be passed a delimiter"); } var delimiter = value[position]; ++position; var closing = GetDelimiterSymbol( GetDelimeterMapping(delimiter), value.Segment(start, position - start)); if (closing == null) { throw new TexParseException($"Cannot find delimiter named {delimiter}"); } closedDelimiter = true; return(closing); } case "sqrt": { // Command is radical. SkipWhiteSpace(value, ref position); TexFormula degreeFormula = null; if (value.Length > position && value[position] == leftBracketChar) { // Degree of radical is specified. degreeFormula = Parse( ReadElementGroup(value, ref position, leftBracketChar, rightBracketChar), formula.TextStyle, environment.CreateChildEnvironment()); } var sqrtFormula = this.Parse( ReadElement(value, ref position), formula.TextStyle, environment.CreateChildEnvironment()); source = value.Segment(start, position - start); return(new Radical(source, sqrtFormula.RootAtom, degreeFormula?.RootAtom)); } case "color": { var colorName = ReadElement(value, ref position); if (!predefinedColors.TryGetValue(colorName.ToString(), out var color)) { throw new TexParseException($"Color {colorName} not found"); } var bodyValue = ReadElement(value, ref position); var bodyFormula = Parse(bodyValue, formula.TextStyle, environment.CreateChildEnvironment()); source = value.Segment(start, position - start); return(new StyledAtom(source, bodyFormula.RootAtom, null, new SolidColorBrush(color))); } case "colorbox": { var colorName = ReadElement(value, ref position); var remainingString = ReadElement(value, ref position); var remaining = Parse(remainingString, formula.TextStyle, environment.CreateChildEnvironment()); if (predefinedColors.TryGetValue(colorName.ToString(), out var color)) { source = value.Segment(start, position - start); return(new StyledAtom(source, remaining.RootAtom, new SolidColorBrush(color), null)); } throw new TexParseException($"Color {colorName} not found"); } } if (environment.AvailableCommands.TryGetValue(command, out var parser) || _commandRegistry.TryGetValue(command, out parser)) { var context = new CommandContext(this, formula, environment, value, start, position); var parseResult = parser.ProcessCommand(context); if (parseResult.NextPosition < position) { throw new TexParseException( $"Incorrect parser behavior for command {command}: NextPosition = {parseResult.NextPosition}, position = {position}. Parser did not made any progress."); } position = parseResult.NextPosition; return(parseResult.Atom); } throw new TexParseException("Invalid command."); }
public MatrixInternalEnvironment( ICommandEnvironment parentEnvironment, List <List <Atom> > rows) : base(parentEnvironment.CreateChildEnvironment(), GetCommands(rows)) { _rows = rows; }