summaryrefslogtreecommitdiff
path: root/support/nlatexdb/nlatexdb/CommandParser.cs
blob: 45b60a2ce548abb7a62822bd1c71f92353baa004 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
//    nlatexdb Version 0.03
//    Database Access in LaTeX
//    Copyright (C) 2011 Robin Höns, Integranova GmbH
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.
//
//    For more information see the web page http://hoens.net/robin



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace nlatexdb
{
    class CommandParser
    {
        public delegate void ExecCommand(List<List<string>> pars);


        public CommandParser(string commandname, int nbbraces, bool lastbraceislatex, ExecCommand cmd)
        {
            m_commandsearch = String.Concat("\\", commandname, "{");
            m_nbbraces = nbbraces;
            m_lastbraceislatex = lastbraceislatex;
            m_cmd = cmd;
        }

        public int FirstIndexIn(string line, int commentbegin)
        {
            if (commentbegin >= 0)
            {
                return line.IndexOf(m_commandsearch, 0, commentbegin);
            }
            return line.IndexOf(m_commandsearch);
        }

        public int StartProcess(string line, int foundindex)
        {
            int myindex = foundindex + m_commandsearch.Length;
            m_bracedepth = 1;
            m_bracescompleted = 0;
            m_pars = new List<List<string>>();
            m_pars.Add(new List<string>());
            return FindBraces(line, myindex);
        }

        public int ProcessLine(string line)
        {
            return FindBraces(line, 0);
        }

        private int FindBraces(string line, int startindex)
        {
            int i = startindex;
            StringBuilder sb = new StringBuilder();
            bool backslash = false;
            while (i < line.Length)
            {
                if (backslash)
                {
                    backslash = false;
                }
                else if (line[i] == '\\')
                {
                    backslash = true;
                }
                else if (line[i] == '%')
                {
                    // Ein Kommentarzeichen. Der Rest der Zeile wird ignoriert, wenn wir in Latex sind
                    if (m_lastbraceislatex && (m_bracescompleted == m_nbbraces - 1))
                    {
                        sb.Append(line.Substring(i));
                        i = line.Length;
                        break;
                    }
                }
                else if (line[i] == '{')
                {
                    m_bracedepth++;
                }
                else if (line[i] == '}')
                {
                    m_bracedepth--;
                    if (m_bracedepth == 0)
                    {
                        m_pars[m_bracescompleted].Add(sb.ToString());
                        m_bracescompleted++;
                        if (m_bracescompleted == m_nbbraces)
                        {
                            // Alles fertig geparst. Rufe Kommando auf

                            m_cmd(m_pars);

                            // return Index nach Befehl
                            i++;
                            return i;
                        }
                        else
                        {
                            // nächstes Klammerpaar. Hier MUSS jetzt wieder eine Klammer aufgehen!
                            i++;
                            if (line[i] != '{')
                            {
                                throw new ParseErrorException("Expecting '{' directly after '}'", line, i);
                            }
                            m_pars.Add(new List<string>());
                            m_bracedepth = 1;
                            sb = new StringBuilder();
                            // Die { nicht zu sb hinzufügen, deshalb continue
                            i++;
                            continue;
                        }
                    }

                }
                sb.Append(line[i]);
                i++;
            }
            m_pars[m_bracescompleted].Add(sb.ToString());
            return -1; // Command still active
        }

        private string m_commandsearch;
        private int m_nbbraces;
        private int m_bracedepth;
        private int m_bracescompleted;
        private bool m_lastbraceislatex;

        private List<List<string>> m_pars;
        private ExecCommand m_cmd;
    }
}