wxMaxima
Loading...
Searching...
No Matches
MaximaTokenizer.h
Go to the documentation of this file.
1// -*- mode: c++; c-file-style: "linux"; c-basic-offset: 2; indent-tabs-mode: nil -*-
2//
3// Copyright (C) 2004-2015 Andrej Vodopivec <andrej.vodopivec@gmail.com>
4// (C) 2014-2016 Gunter Königsmann <wxMaxima@physikbuch.de>
5//
6// This program is free software; you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation; either version 2 of the License, or
9// (at your option) any later version.
10//
11// This program is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15//
16//
17// You should have received a copy of the GNU General Public License
18// along with this program; if not, write to the Free Software
19// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20//
21// SPDX-License-Identifier: GPL-2.0+
22
23#ifndef MAXIMATOKENIZER_H
24#define MAXIMATOKENIZER_H
25
26#include <utility>
27#include <vector>
28#include <memory>
29#include <wx/wx.h>
30#include <wx/string.h>
31#include <wx/arrstr.h>
32#include "precomp.h"
33#include "Compat.h" // wxWARN_UNUSED
34#include "cells/TextStyle.h"
35#include "Configuration.h"
36#include <unordered_map>
37
52{
53public:
59 MaximaTokenizer(const wxString &commands, const Configuration * const configuration);
60
62 // A token is a value: it holds the text and its style and does nothing
63 // else, so an unused one is dead code. The wxString member makes it
64 // non-trivial, so only this attribute can catch that.
66 {
67 public:
68 Token() = default;
69 explicit Token(wxString&& text) : m_text(std::move(text)) {}
70 explicit Token(const wxString &text) : m_text(text) {}
71 Token(wxString&& text, TextStyle style) : m_text(std::move(text)), m_style(style) {}
72 Token(const wxString& text, TextStyle style) : m_text(text), m_style(style) {}
73 Token& operator=(Token&& t) noexcept { m_text = std::move(t.m_text); m_style = t.m_style; return *this; }
74 Token& operator=(const Token& t) { m_text = t.m_text; m_style = t.m_style; return *this; }
75 Token(Token&& token) noexcept { *this = std::move(token); }
76 Token(const Token &token) { *this = token ;}
77 TextStyle GetTextStyle() const { return m_style; }
78 const wxString &GetText() const { return m_text; }
79 operator const wxString &() const { return m_text; }
80 private:
81 wxString m_text;
82 TextStyle m_style = TS_CODE_DEFAULT;
83 };
84 static bool IsAlpha(wxUniChar ch);
85 static bool IsNum(wxUniChar ch);
86 static bool IsAlphaNum(wxUniChar ch);
87 static bool IsSpace(wxUniChar ch);
88 static const wxString UnicodeNumbers() {
89 return wxS("\u00BD\u00B2\u00B3\u221E"); // VULGAR FRACTION ONE HALF, SUPERSCRIPT TWO, SUPERSCRIPT THREE, INFINITY
90 }
91 static const wxString Operators() {
92 return wxS("\u221A\u22C0\u22C1\u22BB\u22BC\u22BD\u00AC\u222b\u2264\u2265\u2211"
93 "\u2260+-*/^:=#'!()[]{}");
94 }
95
96 using TokenList = std::vector<Token>;
97 TokenList PopTokens() && { return std::move(m_tokens); }
98
100 MaximaTokenizer(const wxString &commands, const Configuration * const configuration,
101 const TokenList &initialTokens);
102
114 static bool IsHardcodedKeyword(const wxString &text) {
116 return m_hardcodedFunctions.find(text) != m_hardcodedFunctions.end();
117 }
118
119protected:
121 TokenList m_tokens;
123 static const wxString m_additional_alphas;
125 static const wxString m_not_alphas;
127 static const wxString m_spaces;
129 static const wxString m_plusSigns;
131 static const wxString m_minusSigns;
133 static const wxString m_linebreaks;
134
135 const Configuration * const m_configuration = NULL;
136
137 typedef std::unordered_map <wxString, int, wxStringHash> StringHash;
145 static StringHash m_hardcodedFunctions;
148
149};
150
151#endif // MAXIMATOKENIZER_H
Small compatibility shims for older toolchains and older wxWidgets.
#define wxWARN_UNUSED
Marks a class whose unused instances are a bug.
Definition: Compat.h:116
This file declares everything needed for the text style system used to style all the elements on the ...
TextStyle
All text styles known to wxMaxima.
Definition: TextStyle.h:235
The configuration storage for the current worksheet.
Definition: Configuration.h:98
A maxima code snippet from this tokenizer.
Definition: MaximaTokenizer.h:66
Maximatokenizer breaks down maxima input to individual commands.
Definition: MaximaTokenizer.h:52
static const wxString m_plusSigns
Plus sign.
Definition: MaximaTokenizer.h:129
static StringHash m_hardcodedFunctions
Names of functions that don't require parenthesis.
Definition: MaximaTokenizer.h:145
static const wxString m_not_alphas
Unicode Operators and other special non-ascii characters.
Definition: MaximaTokenizer.h:125
static const wxString m_spaces
Space characters.
Definition: MaximaTokenizer.h:127
static const wxString m_minusSigns
Minus sign.
Definition: MaximaTokenizer.h:131
TokenList m_tokens
The tokens the string is divided into.
Definition: MaximaTokenizer.h:121
static const wxString m_additional_alphas
ASCII symbols that wxIsalnum() doesn't see as chars, but maxima does.
Definition: MaximaTokenizer.h:123
static bool IsHardcodedKeyword(const wxString &text)
Is this text one of the fake-function keywords (see m_hardcodedFunctions)?
Definition: MaximaTokenizer.h:114
static const wxString m_linebreaks
Linebreak characters.
Definition: MaximaTokenizer.h:133
static void EnsureHardcodedFunctionsInitialized()
Populates m_hardcodedFunctions on first use; safe to call repeatedly.
Definition: MaximaTokenizer.cpp:36