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
72 static wxString FirstStatement(const wxString &commands,
73 const Configuration * const configuration);
74
76 // A token is a value: it holds the text and its style and does nothing
77 // else, so an unused one is dead code. The wxString member makes it
78 // non-trivial, so only this attribute can catch that.
80 {
81 public:
82 Token() = default;
83 explicit Token(wxString&& text) : m_text(std::move(text)) {}
84 explicit Token(const wxString &text) : m_text(text) {}
85 Token(wxString&& text, TextStyle style) : m_text(std::move(text)), m_style(style) {}
86 Token(const wxString& text, TextStyle style) : m_text(text), m_style(style) {}
87 Token& operator=(Token&& t) noexcept { m_text = std::move(t.m_text); m_style = t.m_style; return *this; }
88 Token& operator=(const Token& t) = default;
89 Token(Token&& token) noexcept { *this = std::move(token); }
90 Token(const Token &token) { *this = token ;}
91 TextStyle GetTextStyle() const { return m_style; }
92 const wxString &GetText() const { return m_text; }
93 operator const wxString &() const { return m_text; }
94 private:
95 wxString m_text;
96 TextStyle m_style = TS_CODE_DEFAULT;
97 };
98 static bool IsAlpha(wxUniChar ch);
99 static bool IsNum(wxUniChar ch);
100 static bool IsAlphaNum(wxUniChar ch);
101 static bool IsSpace(wxUniChar ch);
102 static const wxString UnicodeNumbers() {
103 return wxS("\u00BD\u00B2\u00B3\u221E"); // VULGAR FRACTION ONE HALF, SUPERSCRIPT TWO, SUPERSCRIPT THREE, INFINITY
104 }
105 static const wxString Operators() {
106 return wxS("\u221A\u22C0\u22C1\u22BB\u22BC\u22BD\u00AC\u222b\u2264\u2265\u2211"
107 "\u2260+-*/^:=#'!()[]{}");
108 }
109
110 using TokenList = std::vector<Token>;
111 TokenList PopTokens() && { return std::move(m_tokens); }
112
114 MaximaTokenizer(const wxString &commands, const Configuration * const configuration,
115 const TokenList &initialTokens);
116
128 static bool IsHardcodedKeyword(const wxString &text) {
130 return m_hardcodedFunctions.contains(text);
131 }
132
133protected:
135 TokenList m_tokens;
137 static const wxString m_additional_alphas;
139 static const wxString m_not_alphas;
141 static const wxString m_spaces;
143 static const wxString m_plusSigns;
145 static const wxString m_minusSigns;
147 static const wxString m_linebreaks;
148
149 const Configuration * const m_configuration = nullptr;
150
151 using StringHash = std::unordered_map<wxString, int, wxStringHash>;
159 static StringHash m_hardcodedFunctions;
162
163};
164
165#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:230
The configuration storage for the current worksheet.
Definition: Configuration.h:102
A maxima code snippet from this tokenizer.
Definition: MaximaTokenizer.h:80
Maximatokenizer breaks down maxima input to individual commands.
Definition: MaximaTokenizer.h:52
static const wxString m_plusSigns
Plus sign.
Definition: MaximaTokenizer.h:143
static StringHash m_hardcodedFunctions
Names of functions that don't require parenthesis.
Definition: MaximaTokenizer.h:159
static const wxString m_not_alphas
Unicode Operators and other special non-ascii characters.
Definition: MaximaTokenizer.h:139
static const wxString m_spaces
Space characters.
Definition: MaximaTokenizer.h:141
static const wxString m_minusSigns
Minus sign.
Definition: MaximaTokenizer.h:145
TokenList m_tokens
The tokens the string is divided into.
Definition: MaximaTokenizer.h:135
static const wxString m_additional_alphas
ASCII symbols that wxIsalnum() doesn't see as chars, but maxima does.
Definition: MaximaTokenizer.h:137
static wxString FirstStatement(const wxString &commands, const Configuration *const configuration)
The first statement in a piece of Maxima code, without its terminator.
Definition: MaximaTokenizer.cpp:400
static bool IsHardcodedKeyword(const wxString &text)
Is this text one of the fake-function keywords (see m_hardcodedFunctions)?
Definition: MaximaTokenizer.h:128
static const wxString m_linebreaks
Linebreak characters.
Definition: MaximaTokenizer.h:147
static void EnsureHardcodedFunctionsInitialized()
Populates m_hardcodedFunctions on first use; safe to call repeatedly.
Definition: MaximaTokenizer.cpp:36