Skip to content

Commit

Permalink
Add simple lexer for Mathematica, issue #652.
Browse files Browse the repository at this point in the history
  • Loading branch information
zufuliu committed Jul 5, 2023
1 parent 1998a49 commit 946d3d3
Show file tree
Hide file tree
Showing 19 changed files with 322 additions and 9 deletions.
2 changes: 2 additions & 0 deletions build/CodeLite/Notepad2.project
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
<File Name="../../scintilla/lexers/LexLua.cxx"/>
<File Name="../../scintilla/lexers/LexMakefile.cxx"/>
<File Name="../../scintilla/lexers/LexMarkdown.cxx"/>
<File Name="../../scintilla/lexers/LexMathematica.cxx"/>
<File Name="../../scintilla/lexers/LexMatlab.cxx"/>
<File Name="../../scintilla/lexers/LexNim.cxx"/>
<File Name="../../scintilla/lexers/LexNSIS.cxx"/>
Expand Down Expand Up @@ -236,6 +237,7 @@
<File Name="../../src/EditLexers/stlLua.c"/>
<File Name="../../src/EditLexers/stlMake.c"/>
<File Name="../../src/EditLexers/stlMarkdown.c"/>
<File Name="../../src/EditLexers/stlMathematica.c"/>
<File Name="../../src/EditLexers/stlMATLAB.c"/>
<File Name="../../src/EditLexers/stlNim.c"/>
<File Name="../../src/EditLexers/stlNsis.c"/>
Expand Down
2 changes: 2 additions & 0 deletions build/VS2017/Notepad2.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,7 @@
<ClCompile Include="..\..\scintilla\lexers\LexLua.cxx" />
<ClCompile Include="..\..\scintilla\lexers\LexMakefile.cxx" />
<ClCompile Include="..\..\scintilla\lexers\LexMarkdown.cxx" />
<ClCompile Include="..\..\scintilla\lexers\LexMathematica.cxx" />
<ClCompile Include="..\..\scintilla\lexers\LexMatlab.cxx" />
<ClCompile Include="..\..\scintilla\lexers\LexNim.cxx" />
<ClCompile Include="..\..\scintilla\lexers\LexNSIS.cxx" />
Expand Down Expand Up @@ -639,6 +640,7 @@
<ClCompile Include="..\..\src\EditLexers\stlLua.c" />
<ClCompile Include="..\..\src\EditLexers\stlMake.c" />
<ClCompile Include="..\..\src\EditLexers\stlMarkdown.c" />
<ClCompile Include="..\..\src\EditLexers\stlMathematica.c" />
<ClCompile Include="..\..\src\EditLexers\stlMATLAB.c" />
<ClCompile Include="..\..\src\EditLexers\stlNsis.c" />
<ClCompile Include="..\..\src\EditLexers\stlNim.c" />
Expand Down
6 changes: 6 additions & 0 deletions build/VS2017/Notepad2.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,9 @@
<ClCompile Include="..\..\scintilla\lexers\LexMarkdown.cxx">
<Filter>Scintilla\lexers</Filter>
</ClCompile>
<ClCompile Include="..\..\scintilla\lexers\LexMathematica.cxx">
<Filter>Scintilla\lexers</Filter>
</ClCompile>
<ClCompile Include="..\..\scintilla\lexers\LexMatlab.cxx">
<Filter>Scintilla\lexers</Filter>
</ClCompile>
Expand Down Expand Up @@ -525,6 +528,9 @@
<ClCompile Include="..\..\src\EditLexers\stlMarkdown.c">
<Filter>Source Files\EditLexers</Filter>
</ClCompile>
<ClCompile Include="..\..\src\EditLexers\stlMathematica.c">
<Filter>Source Files\EditLexers</Filter>
</ClCompile>
<ClCompile Include="..\..\src\EditLexers\stlMATLAB.c">
<Filter>Source Files\EditLexers</Filter>
</ClCompile>
Expand Down
19 changes: 14 additions & 5 deletions doc/FileExt.txt
Original file line number Diff line number Diff line change
Expand Up @@ -290,11 +290,6 @@ F# Source
fsscript F# Script
fsl
fsy
m Mathematica Module
ml OCaml
mli OCaml
sml OCaml
nb Mathematica Notebook


GN Build Script
Expand Down Expand Up @@ -418,6 +413,12 @@ Markdown

litcoffee Literate CoffeeScript

Mathematica
m Module
nb Notebook
wl WolframScript
wls WolframScript

MATLAB Code
m MATLAB / Octave
sce Scilab
Expand All @@ -436,6 +437,14 @@ Nsis Setup Script
nlf NSIS Language File


OCaml Source
ml
mli
mll
mly
sml Standard ML


Pascal Source
pas
inc
Expand Down
3 changes: 3 additions & 0 deletions doc/Notepad2 DarkTheme.ini
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,9 @@ Operator=fore:#A349A4
Variable=fore:#0080FF
Operator=fore:#A349A4
Target=fore:#0080FF; back:#FFC000
[Mathematica]
Number=fore:#F84C4C
Operator=fore:#A349A4
[Markdown]
Horizontal Rule=bold; back:#404030; eolfilled
Block Quote=fore:#A46000; back:#424640; eolfilled
Expand Down
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ Latest development builds (artifacts in Release configuration for each compiler
* [GitHub Flavored Markdown](https://github.github.com/gfm/)
* [GitLab Flavored Markdown](https://docs.gitlab.com/ee/user/markdown.html)
* [Pandoc’s Markdown](https://pandoc.org/MANUAL.html#pandocs-markdown)
* [Mathematica](tools/lang/Mathematica.nb)
* MATLAB, [Screenshots](https://github.com/zufuliu/notepad2/wiki/Screenshots#matlab-octave)
* Octave
* Scilab
Expand Down
11 changes: 11 additions & 0 deletions scintilla/include/SciLexer.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
#define SCLEX_AWK 222
#define SCLEX_CSV 223
#define SCLEX_ZIG 224
#define SCLEX_MATHEMATICA 225
#define SCLEX_AUTOMATIC 1000
#define SCE_PY_DEFAULT 0
#define SCE_PY_COMMENTLINE 1
Expand Down Expand Up @@ -1759,4 +1760,14 @@
#define SCE_ZIG_TYPE 15
#define SCE_ZIG_FUNCTION 16
#define SCE_ZIG_FUNCTION_DEFINITION 17
#define SCE_MATHEMATICA_DEFAULT 0
#define SCE_MATHEMATICA_COMMENTLINE 1
#define SCE_MATHEMATICA_COMMENT 2
#define SCE_MATHEMATICA_NUMBER 3
#define SCE_MATHEMATICA_OPERATOR 4
#define SCE_MATHEMATICA_STRING 5
#define SCE_MATHEMATICA_ESCAPECHAR 6
#define SCE_MATHEMATICA_SLOT 7
#define SCE_MATHEMATICA_IDENTIFIER 8
#define SCE_MATHEMATICA_KEYWORD 9
/* --Autogenerated -- end of section automatically generated from SciLexer.iface */
13 changes: 13 additions & 0 deletions scintilla/include/SciLexer.iface
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ val SCLEX_JAMFILE=221
val SCLEX_AWK=222
val SCLEX_CSV=223
val SCLEX_ZIG=224
val SCLEX_MATHEMATICA=225

# When a lexer specifies its language as SCLEX_AUTOMATIC it receives a
# value assigned in sequence from SCLEX_AUTOMATIC+1.
Expand Down Expand Up @@ -3149,3 +3150,15 @@ val SCE_ZIG_WORD=
val SCE_ZIG_TYPE=
val SCE_ZIG_FUNCTION=
val SCE_ZIG_FUNCTION_DEFINITION=
# Lexical states for SCLEX_MATHEMATICA
lex Mathematica=SCLEX_MATHEMATICA SCE_MATHEMATICA_
val SCE_MATHEMATICA_DEFAULT=
val SCE_MATHEMATICA_COMMENTLINE=
val SCE_MATHEMATICA_COMMENT=
val SCE_MATHEMATICA_NUMBER=
val SCE_MATHEMATICA_OPERATOR=
val SCE_MATHEMATICA_STRING=
val SCE_MATHEMATICA_ESCAPECHAR=
val SCE_MATHEMATICA_SLOT=
val SCE_MATHEMATICA_IDENTIFIER=
val SCE_MATHEMATICA_KEYWORD=
177 changes: 177 additions & 0 deletions scintilla/lexers/LexMathematica.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
// This file is part of Notepad2.
// See License.txt for details about distribution and modification.
//! Lexer for Mathematica

#include <cassert>
#include <cstring>

#include <string>
#include <string_view>

#include "ILexer.h"
#include "Scintilla.h"
#include "SciLexer.h"

#include "WordList.h"
#include "LexAccessor.h"
#include "Accessor.h"
#include "StyleContext.h"
#include "CharacterSet.h"
#include "LexerModule.h"

using namespace Lexilla;

namespace {

//KeywordIndex++Autogenerated -- start of section automatically generated
enum {
KeywordIndex_Keyword = 0,
};
//KeywordIndex--Autogenerated -- end of section automatically generated

enum class SlotType {
None,
Sharp, // #name
Backtick, // `name`
};

void ColouriseMathematicaDoc(Sci_PositionU startPos, Sci_Position lengthDoc, int initStyle, LexerWordList keywordLists, Accessor &styler) {
const bool fold = styler.GetPropertyBool("fold");

SlotType slotType = SlotType::None;
int outerState = SCE_MATHEMATICA_DEFAULT;
int commentLevel = 0;
int levelCurrent = SC_FOLDLEVELBASE;

StyleContext sc(startPos, lengthDoc, initStyle, styler);
if (sc.currentLine > 0) {
commentLevel = styler.GetLineState(sc.currentLine - 1);
levelCurrent = styler.LevelAt(sc.currentLine - 1) >> 16;
}

int levelNext = levelCurrent;
if (startPos == 0 && sc.Match('#', '!')) {
// WolframScript
sc.SetState(SCE_MATHEMATICA_COMMENTLINE);
sc.Forward();
}

while (sc.More()) {
switch (sc.state) {
case SCE_MATHEMATICA_OPERATOR:
sc.SetState(SCE_MATHEMATICA_DEFAULT);
break;

case SCE_MATHEMATICA_NUMBER:
if (!IsDecimalNumber(sc.chPrev, sc.ch, sc.chNext)) {
sc.SetState(SCE_MATHEMATICA_DEFAULT);
}
break;

case SCE_MATHEMATICA_IDENTIFIER:
case SCE_MATHEMATICA_SLOT:
if (!IsIdentifierChar(sc.ch)) {
if (sc.state == SCE_MATHEMATICA_IDENTIFIER) {
char s[128];
sc.GetCurrent(s, sizeof(s));
if (keywordLists[0].InList(s)) {
sc.ChangeState(SCE_MATHEMATICA_KEYWORD);
}
sc.SetState(SCE_MATHEMATICA_DEFAULT);
} else {
if (slotType == SlotType::Backtick) {
if (sc.ch != '`') {
sc.ChangeState(SCE_MATHEMATICA_STRING);
} else {
sc.Forward();
}
}
sc.SetState(outerState);
continue;
}
}
break;

case SCE_MATHEMATICA_COMMENT:
if (sc.Match('(', '*')) {
commentLevel++;
sc.Forward();
} else if (sc.Match('*', ')')) {
sc.Forward();
commentLevel--;
if (commentLevel == 0) {
levelNext--;
sc.ForwardSetState(SCE_MATHEMATICA_DEFAULT);
}
}
break;

case SCE_MATHEMATICA_COMMENTLINE:
if (sc.atLineStart) {
sc.SetState(SCE_MATHEMATICA_DEFAULT);
}
break;

case SCE_MATHEMATICA_STRING:
if (sc.ch == '\\') {
sc.Forward();
} else if (sc.ch == '#' || sc.ch == '`') {
// TODO: highlight <* expr *>
outerState = SCE_MATHEMATICA_STRING;
slotType = (sc.ch == '#') ? SlotType::Sharp : SlotType::Backtick;
sc.SetState(SCE_MATHEMATICA_SLOT);
} else if (sc.ch == '\"') {
sc.ForwardSetState(SCE_MATHEMATICA_DEFAULT);
}
break;
}

if (sc.state == SCE_MATHEMATICA_DEFAULT) {
if (sc.Match('(', '*')) {
commentLevel = 1;
levelNext++;
sc.SetState(SCE_MATHEMATICA_COMMENT);
sc.Forward();
} else if (sc.ch == '\"') {
sc.SetState(SCE_MATHEMATICA_STRING);
} else if (IsNumberStart(sc.ch, sc.chNext)) {
sc.SetState(SCE_MATHEMATICA_NUMBER);
} else if (sc.ch == '#') {
outerState = SCE_MATHEMATICA_DEFAULT;
slotType = SlotType::Sharp;
sc.SetState(SCE_MATHEMATICA_SLOT);
} else if (IsIdentifierStart(sc.ch)) {
sc.SetState(SCE_MATHEMATICA_IDENTIFIER);
} else if (IsAGraphic(sc.ch)) {
sc.SetState(SCE_MATHEMATICA_OPERATOR);
if (sc.ch == '{' || sc.ch == '[' || sc.ch == '(') {
levelNext++;
} else if (sc.ch == '}' || sc.ch == ']' || sc.ch == ')') {
levelNext--;
}
}
}
if (sc.atLineEnd) {
if (fold) {
const int levelUse = levelCurrent;
int lev = levelUse | levelNext << 16;
if (levelUse < levelNext) {
lev |= SC_FOLDLEVELHEADERFLAG;
}
if (lev != styler.LevelAt(sc.currentLine)) {
styler.SetLevel(sc.currentLine, lev);
}
}

styler.SetLineState(sc.currentLine, commentLevel);
levelCurrent = levelNext;
}
sc.Forward();
}

sc.Complete();
}

}

LexerModule lmMathematica(SCLEX_MATHEMATICA, ColouriseMathematicaDoc, "mathematica");
2 changes: 2 additions & 0 deletions scintilla/lexlib/LexerModule.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ extern LexerModule lmLLVM;
extern LexerModule lmLua;
extern LexerModule lmMakefile;
extern LexerModule lmMarkdown;
extern LexerModule lmMathematica;
extern LexerModule lmMatlab;
extern LexerModule lmNim;
extern LexerModule lmNsis;
Expand Down Expand Up @@ -148,6 +149,7 @@ const LexerModule * const lexerCatalogue[] = {
&lmLua,
&lmMakefile,
&lmMarkdown,
&lmMathematica,
&lmMatlab,
&lmNim,
&lmNsis,
Expand Down
1 change: 1 addition & 0 deletions src/EditAutoC.c
Original file line number Diff line number Diff line change
Expand Up @@ -2650,6 +2650,7 @@ void EditToggleCommentBlock(void) {
break;

case NP2LEX_FSHARP:
case NP2LEX_MATHEMATICA:
EditEncloseSelection(L"(*", L"*)");
break;

Expand Down
1 change: 1 addition & 0 deletions src/EditLexer.h
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ typedef const EDITLEXER *LPCEDITLEXER;
#define NP2LEX_OCTAVE 63061 // SCLEX_MATLAB Octave Code
#define NP2LEX_SCILAB 63062 // SCLEX_MATLAB SciLab Code
#define NP2LEX_RLANG 63063 // SCLEX_RLANG R Code
#define NP2LEX_MATHEMATICA 63064 // SCLEX_MATHEMATICA Mathematica

#define NP2LEX_JULIA 63066 // SCLEX_JULIA Julia Script
#define NP2LEX_RUST 63067 // SCLEX_RUST Rust Source
Expand Down
2 changes: 1 addition & 1 deletion src/EditLexers/stlFSharp.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ EDITLEXER lexFSharp = {
SCE_FSHARP_CHARACTER, SCE_FSHARP_STRING,
//Settings--Autogenerated -- end of section automatically generated
EDITLEXER_HOLE(L"F# Source", Styles_FSharp),
L"fs; fsi; fsx; fsscript; fsl; fsy; ml; mli; sml; nb",
L"fs; fsi; fsx; fsscript; fsl; fsy",
&Keywords_FSharp,
Styles_FSharp
};
Loading

0 comments on commit 946d3d3

Please sign in to comment.