Ejemplo n.º 1
0
        public StandardCommandProcessor(
            ICommandEnvironment CommandEnvironment,
            ICommandFactory CommandFactory,
            ITerminal  Terminal,
            int        HistorySize)
        {
            _History = new History( HistorySize );

            _CommandEnvironment = CommandEnvironment;

            _Terminal = Terminal;

            _InternalCommandFactory = CommandFactory;

            _Parser = new DefaultCommandParser();

            _CommandList = new CommandList();

            _Prompt = new Prompt( _Terminal, "$p$g" );
        }
Ejemplo n.º 2
0
        private TexFormula Parse(
            SourceSpan value,
            ref int position,
            bool allowClosingDelimiter,
            string textStyle,
            ICommandEnvironment environment)
        {
            var formula = new TexFormula()
            {
                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);
        }
Ejemplo n.º 3
0
        internal TexFormula Parse(SourceSpan value, string textStyle, ICommandEnvironment environment)
        {
            int localPostion = 0;

            return(Parse(value, ref localPostion, false, textStyle, environment));
        }
Ejemplo n.º 4
0
        private Atom AttachScripts(
            TexFormula formula,
            SourceSpan value,
            ref int position,
            Atom atom,
            bool skipWhiteSpace,
            ICommandEnvironment environment)
        {
            if (skipWhiteSpace)
            {
                SkipWhiteSpace(value, ref position);
            }

            var initialPosition = position;

            if (position == value.Length)
            {
                return(atom);
            }

            // Check for prime marks.
            var primesRowAtom = new RowAtom(new SourceSpan(value.Source, position, 0));
            int i             = position;

            while (i < value.Length)
            {
                if (value[i] == primeChar)
                {
                    primesRowAtom = primesRowAtom.Add(SymbolAtom.GetAtom("prime", value.Segment(i, 1)));
                    position++;
                }
                else if (!IsWhiteSpace(value[i]))
                {
                    break;
                }
                i++;
            }

            var primesRowSource = new SourceSpan(
                value.Source,
                primesRowAtom.Source.Start,
                position - primesRowAtom.Source.Start);

            primesRowAtom = primesRowAtom.WithSource(primesRowSource);

            if (primesRowAtom.Elements.Count > 0)
            {
                atom = new ScriptsAtom(primesRowAtom.Source, atom, null, primesRowAtom);
            }

            if (position == value.Length)
            {
                return(atom);
            }

            TexFormula superscriptFormula = null;
            TexFormula subscriptFormula   = null;

            var ch = value[position];

            if (ch == superScriptChar)
            {
                // Attach superscript.
                position++;
                superscriptFormula = ReadScript(formula, value, ref position, environment);

                SkipWhiteSpace(value, ref position);
                if (position < value.Length && value[position] == subScriptChar)
                {
                    // Attach subscript also.
                    position++;
                    subscriptFormula = ReadScript(formula, value, ref position, environment);
                }
            }
            else if (ch == subScriptChar)
            {
                // Add subscript.
                position++;
                subscriptFormula = ReadScript(formula, value, ref position, environment);

                SkipWhiteSpace(value, ref position);
                if (position < value.Length && value[position] == superScriptChar)
                {
                    // Attach superscript also.
                    position++;
                    superscriptFormula = ReadScript(formula, value, ref position, environment);
                }
            }

            if (superscriptFormula == null && subscriptFormula == null)
            {
                return(atom);
            }

            // Check whether to return Big Operator or Scripts.
            var subscriptAtom   = subscriptFormula?.RootAtom;
            var superscriptAtom = superscriptFormula?.RootAtom;

            if (atom.GetRightType() == TexAtomType.BigOperator)
            {
                var source = value.Segment(atom.Source.Start, position - atom.Source.Start);
                if (atom is BigOperatorAtom typedAtom)
                {
                    return(new BigOperatorAtom(
                               source,
                               typedAtom.BaseAtom,
                               subscriptAtom,
                               superscriptAtom,
                               typedAtom.UseVerticalLimits));
                }

                return(new BigOperatorAtom(source, atom, subscriptAtom, superscriptAtom));
            }
            else
            {
                var source = new SourceSpan(value.Source, initialPosition, position - initialPosition);
                return(new ScriptsAtom(source, atom, subscriptAtom, superscriptAtom));
            }
        }
Ejemplo n.º 5
0
        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());

                var source      = value.Segment(start, position - start);
                var atom        = styledFormula.RootAtom ?? new NullAtom(source);
                var commandAtom = AttachScripts(formula, value, ref position, atom, true, environment);
                formula.Add(commandAtom, 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 + "'");
            }
        }
Ejemplo n.º 6
0
        /// <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 color = ReadColorModelData(value, ref position);

                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 color = ReadColorModelData(value, ref position);

                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, new SolidColorBrush(color), null));
            }
            }

            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.");
        }
Ejemplo n.º 7
0
 private TexFormula ReadScript(
     TexFormula formula,
     SourceSpan value,
     ref int position,
     ICommandEnvironment environment) =>
 Parse(ReadElement(value, ref position), formula.TextStyle, environment.CreateChildEnvironment());
 public MatrixInternalEnvironment(
     ICommandEnvironment parentEnvironment,
     List <List <Atom> > rows) : base(parentEnvironment.CreateChildEnvironment(), GetCommands(rows))
 {
     _rows = rows;
 }