Howto:Syntax highlighting for Nasal: Difference between revisions

From FlightGear wiki
Jump to navigation Jump to search
No edit summary
(added visual code extension)
(47 intermediate revisions by 15 users not shown)
Line 1: Line 1:
There's '''[[Nasal]] syntax-highlighting''' support available for some editors, which is a big advantage, as it makes Nasal coding much easier. It can often point to syntax errors and so reduce the number of tedious time-consuming and unproductive [[FlightGear]] runs.
There's '''[[Nasal]] syntax-highlighting''' support available for some editors, which is a big advantage, as it makes Nasal coding much easier.


== Notepad++ ==
Syntax highlighting can often point to syntax errors and so reduce the number of tedious time-consuming and unproductive [[FlightGear]] runs. It can also be a sort of "guide" to the language, highlighting functions or keywords that are builtin, so you can quickly check if it is "type" or "typeof" by typing each. In addition makes understanding other people's code easier, since you have all of the parts visually separated out, like loops, strings, constants, and builtin functions.
http://dl.dropbox.com/u/1463693/nasal.xml


Instructions are here: http://superuser.com/questions/62474/notepad-custom-syntax-highlighting-setting-where-to-look-for-it
== Atom ==
Script is available from [https://github.com/www2000/atom-language-nasal Github] and can also be found in the [https://atom.io/packages/language-nasal Atom's packages repository].


== jEdit ==
Manual installation:
There's a syntax highlighting mode for jEdit, programmer's text editor


Included are some of Nasal's internal functions and those functions currently implemented in NasalSys.cxx
# Download the latest release from https://github.com/www2000/atom-language-nasal
To use, add the content of the catalog inside your own catalog (do NOT overwrite the file) and nasal.xml in /home/USER/.jedit/modes
# Unpack the tar.bz2
# Copy/move the language-nasal package to ~/.atom/packages


Restart jEdit and you can use it. Current extension is .nas, you can add your own extension and functions.
For installation from atom's packages repository:
This mode is heavily based on the Javascript mode.


Get it here: http://www.jedit.org/
# Open Atom
# Go to Preferences Edit -> Preferences or {{key press|Ctrl|+}},
# Click on packages and search for nasal
# Click install for Language Nasal


==gEdit==
== Emacs ==
[[File:Emacs-nasal-syntax-highlighting.png|400px|thumb|Nasal syntax highlighting in GNU Emacs]]


'''Philosopher''' on the FlightGear Forum has created a syntax highlighter for gEdit- a popular and simple Text Editor for Mac OSX and Linux.
There is a Emacs major mode for Nasal available here: https://github.com/andyross/nasal/blob/master/misc/nasal-mode.el


Create a new text file - ''nasal.lang'' with the following contents.
Under Linux just drop the file where you have the rest of your manually installed packages and add the following to your .emacs file:
<nowiki>(require 'nasal-mode)</nowiki>


<nowiki><syntaxhighlight lang="xml">
If you don't have a directory to place your manually installed packages, put the file in the directory ''~/.emacs.d/lisp/'' (create it if it doesn't exist) and then put the following in your .emacs file before the ''require'':
<?xml version="1.0" encoding="UTF-8"?>
<nowiki>;; Tell emacs where is your personal elisp lib dir
<!--
(add-to-list 'load-path "~/.emacs.d/lisp/")</nowiki>


Author: Gustavo Giráldez <gustavo.giraldez@gmx.net>
Reload .emacs via <code>'''M-x''' load-file</code> or by restarting Emacs.
Copyright (C) 2003 Gustavo Giráldez <gustavo.giraldez@gmx.net>
Copyright (C) 2004 Benoît Dejean <TaZForEver@free.fr>
Copyright (C) 2006 Steve Frécinaux <nud@apinc.org>


This library is free software; you can redistribute it and/or
== gedit ==
modify it under the terms of the GNU Library General Public
[[File:Philosopher's nasal highlighting in gEdit.jpeg|400px|thumb|Screen shot illustrating syntax highlighting in gedit]]
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.


This library is distributed in the hope that it will be useful,
'''Philosopher''' on the FlightGear Forum has created syntax highlighting for gedit, a popular and simple text editor for Mac OS X, Linux, and Windows.
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Library General Public License for more details.


You should have received a copy of the GNU Library General Public
<!--http://theomegahangar.flymerlion.com/downloads/nasal.lang -->
License along with this library; if not, write to the
To install, copy the ''nasal.lang'' file <!--from the link above or--> from the first post of the forum topic {{forum link|p=164133}} and:
Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.


-->
* If you use Mac, move it into your ''Applications/gedit/Contents/Resources/share/gtksourceview-X.0/language specs'' folder.
<language id="nasal" _name="Nasal" version="2.0" _section="Scripts">
* For Linux, move it into your ''/usr/share/gtksourceview-X.0/language-specs'' folder.
  <metadata>
    <property name="mimetypes">text/nasal;application/nasal</property>
    <property name="globs">*.nas</property>
    <property name="line-comment-start">#</property>
  </metadata>


  <styles>
In the same post given above, there are instructions for how to edit ''xml.lang'' (in the same directory) to add the embedded XML content support (with this minor edit, Nasal highlighting will be used inside of certain tags, instead of plain text).
    <style id="module-handler"    _name="Module Handler"        map-to="def:preprocessor"/>
    <style id="keyword"          _name="Keyword"              map-to="def:keyword"/>
    <!--<style id="multiline-string"  _name="Multiline string"      map-to="def:string"/>-->
    <style id="string"            _name="String"                map-to="def:string"/>
    <style id="escaped-char"      _name="Escaped Character"    map-to="def:special-char"/>
    <style id="format"            _name="Format"                map-to="def:character"/>
    <style id="string-conversion" _name="string-conversion"/>
    <style id="special-variable"  _name="Special Variable"      map-to="def:identifier"/>
    <style id="boolean"          _name="Boolean"              map-to="def:boolean"/>
    <style id="floating-point"    _name="Floating point number" map-to="def:floating-point"/>
    <style id="decimal"          _name="Decimal number"        map-to="def:decimal"/>
    <style id="base-n-integer"    _name="Base-N number"        map-to="def:base-n-integer"/>
    <style id="complex"          _name="Complex number"        map-to="def:complex"/>
    <style id="builtin-constant"  _name="Builtin Constant"      map-to="def:special-constant"/>
    <style id="builtin-object"    _name="Builtin Object"        map-to="def:type"/>
    <style id="builtin-function"  _name="Builtin Function"      map-to="def:builtin"/>
    <style id="path"              _name="Path"                  map-to="def:string"/>
  </styles>


  <definitions>
For "snippets" support, nasal.xml from the above post has to be moved into ''/usr/share/gedit/plugins/snippets/'' folder or installed using the "import" feature. See this image for more details:
    <define-regex id="identifier">[_a-zA-Z][_a-zA-Z0-9]*</define-regex>
    <define-regex id="number">[1-9][0-9]*</define-regex>


    <!-- http://docs.python.org/lib/typesseq-strings.html -->
[[:File:Install nasal snippets in gedit.png|how to import Nasal snippets]].
    <context id="format" style-ref="format" extend-parent="false">
{{-}}
      <match extended="true">
        %                      # leading % sign
        \(\%{identifier}\)?    # mapping key
        [#0\-\ \+]*            # conversion flags
        (\-?\%{number}|\*)?    # minimum field width
        (\.(\-?\%{number}|\*))? # precision
        (hlL)?                  # lentgh modifier
        [diouxXeEfFgGcrs%]      # conversion type
      </match>
    </context>


    <!-- http://docs.python.org/ref/strings.html -->
== jEdit ==
    <context id="escaped-char" style-ref="escaped-char" extend-parent="true">
There's a syntax highlighting mode for jEdit, programmer's text editor
      <match extended="true">
        \\(                # leading backslash
        [\\'"abfnrtv]    | # single escaped char
        N\{[A-Z\ ]+\}    | # named unicode character
        u[0-9A-Fa-f]{4}  | # xxxx - character with 16-bit hex value xxxx
        U[0-9A-Fa-f]{8}  | # xxxxxxxx - character with 32-bit hex value xxxxxxxx
        x[0-9A-Fa-f]{1,2} | # \xhh - character with hex value hh
        [0-7]{1,3}          # \ooo - character with octal value ooo
        )
      </match>
    </context>


    <define-regex id="string-prefix">(u|U)?</define-regex>
Included are some of Nasal's internal functions and those functions currently implemented in NasalSys.cxx
    <define-regex id="raw-string-prefix">(r|ur|R|UR|Ur|uR)</define-regex>
To use, add the content of the catalog inside your own catalog (do NOT overwrite the file) and nasal.xml in /home/USER/.jedit/modes


    <!--<context id="multiline-double-quoted-string" style-ref="multiline-string" class="string" class-disabled="no-spell-check">
Restart jEdit and you can use it. Current extension is .nas, you can add your own extension and functions.
      <start>\%{string-prefix}"""</start>
This mode is heavily based on the Javascript mode.
      <end>"""</end>
      <include>
        <context ref="format"/>
        <context ref="escaped-char"/>
      </include>
    </context>


    <context id="multiline-single-quoted-string" style-ref="multiline-string" class="string" class-disabled="no-spell-check">
Get it here: https://sourceforge.net/projects/fgscheduleview/files/jedit/catalog.zip/download
      <start>\%{string-prefix}'''</start>
      <end>'''</end>
      <include>
        <context ref="format"/>
        <context ref="escaped-char"/>
      </include>
    </context>-->


    <context id="double-quoted-string" style-ref="string" end-at-line-end="true" class="string" class-disabled="no-spell-check">
== KDE Editors ==
      <start>\%{string-prefix}"</start>
There's a highlighting definition for nasal available at '''opendesktop.org''' (https://www.opendesktop.org/p/1187022/)
      <end>"</end>
      <include>
        <context ref="format"/>
        <context ref="escaped-char"/>
        <context ref="def:line-continue"/>
      </include>
    </context>


    <context id="single-quoted-string" style-ref="string" end-at-line-end="true" class="string" class-disabled="no-spell-check">
Download the file and copy it into:
      <start>\%{string-prefix}'</start>
:~/.kde4/share/apps/katepart/syntax/ (for KDE4)
      <end>'</end>
:or
      <include>
:~/.local/share/katepart5/syntax/ (for KF5 pre 5.29)
        <context ref="format"/>
:or
        <context ref="escaped-char"/>
:~/.local/share/org.kde.syntax.highlighting/syntax/ (for current KF5 versions)
        <context ref="def:line-continue"/>
      </include>
    </context>


    <!--<context id="multiline-double-quoted-raw-string" style-ref="multiline-string" class="string" class-disabled="no-spell-check">
(you can create the folders if they don't exist yet).
      <start>\%{raw-string-prefix}"""</start>
      <end>"""</end>
      <include>
        <context ref="format"/>
      </include>
    </context>


    <context id="multiline-single-quoted-raw-string" style-ref="multiline-string" class="string" class-disabled="no-spell-check">
Note that the syntax highlighting in KDE doesn't set colors, it just identifies structural elements, colors are defined by the '''schemas''' used by each editor, thus you get consistent highlighting between different languages.
      <start>\%{raw-string-prefix}'''</start>
      <end>'''</end>
      <include>
        <context ref="format"/>
      </include>
    </context>-->


    <context id="double-quoted-raw-string" style-ref="string" end-at-line-end="true" class="string" class-disabled="no-spell-check">
The highlighting file should work with all editors based on the katepart: KWrite, Kate, and the editor component of KDevelop.
      <start>\%{raw-string-prefix}"</start>
      <end>"</end>
      <include>
        <context ref="format"/>
        <context ref="def:line-continue"/>
      </include>
    </context>


    <context id="single-quoted-raw-string" style-ref="string" end-at-line-end="true" class="string" class-disabled="no-spell-check">
== Notepad++ ==
      <start>\%{raw-string-prefix}'</start>
[[File:Highlight parse.png|400px|thumb|Screenshot of Nasal support for Notepad++]]
      <end>'</end>
Provides comprehensive syntax highlighting and a function list parser with support for hierarchical display of both inline and out-of-body class member functions.
      <include>
* [http://github.com/Slaiyer/nasal-npp GitHub repository for download and instructions]
        <context ref="format"/>
        <context ref="def:line-continue"/>
      </include>
    </context>


    <context id="nasal" class="no-spell-check">
'''Alternative (syntax highlighting only):'''
      <include>
* [http://dl.dropbox.com/u/1463693/nasal.xml Download here]
        <context ref="def:shebang"/>
** [http://superuser.com/questions/62474/notepad-custom-syntax-highlighting-setting-where-to-look-for-it Instructions are here]
        <context ref="def:shell-like-comment"/>
{{-}}


        <!--<context ref="multiline-double-quoted-string"/>
== Sublime Text 2 ==
        <context ref="multiline-single-quoted-string"/>-->
[[File:Sublime Text 2 syntax highlighting.png|400px|thumb|Sublime Text 2 syntax highlighting]]
        <context ref="double-quoted-string"/>
        <context ref="single-quoted-string"/>


        <!--<context ref="multiline-double-quoted-raw-string"/>
Scripts and installation instructions are here: https://github.com/freevryheid/nasal
        <context ref="multiline-single-quoted-raw-string"/>-->
{{-}}
        <context ref="double-quoted-raw-string"/>
        <context ref="single-quoted-raw-string"/>


        <context id="string-conversion" style-ref="string-conversion" end-at-line-end="true">
== Vim ==
          <start>`</start>
[[File:Vim-nasal-syntax-highlighting.png|400px|thumb|Screen shot illustrating syntax highlighting in Vim]]
          <end>`</end>
          <include>
            <context ref="nasal"/>
          </include>
        </context>


        <!--<context id="special-variables" style-ref="special-variable">
One such editor is the free vim or its GUI variant gvim.  
          <prefix>(?&lt;![\w\.])</prefix>
          <keyword>self</keyword>
          <keyword>__name__</keyword>
          <keyword>__debug__</keyword>
        </context>-->


        <context id="boolean" style-ref="boolean">
It's not for everyone but it's free, and testing it doesn't hurt: http://www.vim.org/.
          <prefix>(?&lt;![\w\.])</prefix>
          <keyword>false</keyword>
          <keyword>true</keyword>
        </context>


        <define-regex id="path" extended="true"><!-- matches a path inside of a string like "/path[0]/node" or "path[1]/node_now" -->
The syntax definition file comes with the FlightGear code ({{flightgear file|scripts/syntax/nasal.vim}}). Highlighting works even for Nasal embedded in XML files (type ":set ft=nasal", where ft stands for file-type)
          "[/\[]\w]{0,1000}"
        </define-regex>


        <define-regex id="float" extended="true">
Here's an example, which demonstrates a short code segment with three syntax errors as well as the highlighting of a matching pair of parentheses (yellow) and trailing spaces (blue x). (The leading blue dots aren't on by default. They help to spot tab crimes.)
          ( (\d+)?\.\d+ | \d+\. ) |
{{-}}
          ( (\d+|(\d+)?\.\d+|\d+\.)[eE][+-]?\d+ )
        </define-regex>
 
        <context id="complex" style-ref="complex">
          <match>(?&lt;![\w\.])(\%{float}|\d+)[jJ]\b</match>
        </context>
 
        <context id="float" style-ref="floating-point">
          <match>(?&lt;![\w\.])\%{float}(?![\w\.])</match>
        </context>
 
        <context id="decimal" style-ref="decimal">
          <match>(?&lt;![\w\.])([1-9][0-9]*|0)[lL]?(?![\w\.])</match>
        </context>
 
        <context id="octal" style-ref="base-n-integer">
          <match>(?&lt;![\w\.])0[0-7]+[lL]?(?![\w\.])</match>
        </context>
 
        <context id="hex" style-ref="base-n-integer">
          <match>(?&lt;![\w\.])0[xX][0-9A-Fa-f]+[lL]?(?![\w\.])</match>
        </context>
 
        <context id="module-handler" style-ref="module-handler">
          <keyword>import</keyword>
          <keyword>from</keyword>
          <keyword>as</keyword>
        </context>


        <context id="keyword" style-ref="keyword">
== Visual Studio Code ==
          <keyword>and</keyword>
[[File:Vscode-sintaxhighlight.jpg|thumb|Screen shot illustrating syntax highlighting in VSCode]]
          <keyword>or</keyword>
          <keyword>var</keyword>
          <keyword>for</keyword>
          <keyword>foreach</keyword>
          <keyword>forindex</keyword>
          <keyword>elsif</keyword>
          <keyword>else</keyword>
          <keyword>if</keyword>
          <keyword>while</keyword>
          <keyword>func</keyword>
          <keyword>print</keyword>
          <keyword>return</keyword>
        </context>


        <context id="builtin-constants" style-ref="builtin-constant">
[https://code.visualstudio.com Visual Studio Code] is a source-code editor developed by Microsoft for Windows, Linux and macOS. Its open source and free.
          <prefix>(?&lt;![\w\.])</prefix>
          <keyword>math.sin</keyword>
          <keyword>math.cos</keyword>
          <keyword>math.exp</keyword>
          <keyword>math.ln</keyword>
          <keyword>math.sqrt</keyword>
          <keyword>math.atan2</keyword>
          <keyword>nil</keyword>
        </context>


        <!--<context id="builtin-objects" style-ref="builtin-object">
The nasal extension is available from [https://github.com/RenanMsV/nasal-vscode Github] and can also be found in the [https://marketplace.visualstudio.com/items?itemName=RenanMsV.nasal-lang VSCode's marketplace].
          <prefix>(?&lt;![\w\.])</prefix>
It supports basic syntax highlighting and snippets to improve development speed.
          <keyword>ArithmeticError</keyword>
          <keyword>AssertionError</keyword>
          <keyword>AttributeError</keyword>
          <keyword>EnvironmentError</keyword>
          <keyword>EOFError</keyword>
          <keyword>Exception</keyword>
          <keyword>FloatingPointError</keyword>
          <keyword>ImportError</keyword>
          <keyword>IndentationError</keyword>
          <keyword>IndexError</keyword>
          <keyword>IOError</keyword>
          <keyword>KeyboardInterrupt</keyword>
          <keyword>KeyError</keyword>
          <keyword>LookupError</keyword>
          <keyword>MemoryError</keyword>
          <keyword>NameError</keyword>
          <keyword>NotImplementedError</keyword>
          <keyword>OSError</keyword>
          <keyword>OverflowError</keyword>
          <keyword>ReferenceError</keyword>
          <keyword>RuntimeError</keyword>
          <keyword>StandardError</keyword>
          <keyword>StopIteration</keyword>
          <keyword>SyntaxError</keyword>
          <keyword>SystemError</keyword>
          <keyword>SystemExit</keyword>
          <keyword>TabError</keyword>
          <keyword>TypeError</keyword>
          <keyword>UnboundLocalError</keyword>
          <keyword>UnicodeDecodeError</keyword>
          <keyword>UnicodeEncodeError</keyword>
          <keyword>UnicodeError</keyword>
          <keyword>UnicodeTranslateError</keyword>
          <keyword>ValueError</keyword>
          <keyword>WindowsError</keyword>
          <keyword>ZeroDivisionError</keyword>
          <keyword>Warning</keyword>
          <keyword>UserWarning</keyword>
          <keyword>DeprecationWarning</keyword>
          <keyword>PendingDeprecationWarning</keyword>
          <keyword>SyntaxWarning</keyword>
          <keyword>OverflowWarning</keyword>
          <keyword>RuntimeWarning</keyword>
          <keyword>FutureWarning</keyword>
        </context>-->


        <context id="builtin-function" style-ref="builtin-function">
For installation from the marketplace:
          <prefix>(?&lt;![\w\.])</prefix> <!-- look behind for anything but a word or period as a prefix, e.g. a space or tab -->
          <!--<keyword>setattr</keyword>
          <keyword>getattr</keyword>-->
          <keyword>setlistener</keyword>
          <keyword>setprop</keyword>
          <keyword>getprop</keyword>
          <keyword>settimer</keyword>
          <keyword>append</keyword>
          <keyword>setsize</keyword>
          <keyword>subvec</keyword>
          <keyword>contains</keyword>
          <keyword>delete</keyword>
          <keyword>num</keyword>
          <keyword>keys</keyword>
          <keyword>pop</keyword>
          <keyword>size</keyword>
          <keyword>streq</keyword>
          <keyword>cmp</keyword>
          <keyword>sort</keyword>
          <keyword>substr</keyword>
          <keyword>sprintf</keyword>
          <keyword>find</keyword>
          <keyword>split</keyword>
          <keyword>rand</keyword>
          <keyword>typeof</keyword>
          <keyword>die</keyword>
          <keyword>call</keyword>
          <keyword>compile</keyword>
          <keyword>closure</keyword>
          <keyword>bind</keyword>
          <keyword>print</keyword>
          <keyword>fgcommand</keyword>
          <keyword>interpolate</keyword>
          <keyword>gui.Dialog.new</keyword>
          <keyword>props.Node.new</keyword>
          <keyword>props.globals</keyword>
          <keyword>props.globals.getNode</keyword>
          <keyword>props.dump</keyword>
          <keyword>bits.fld</keyword>
          <keyword>bits.sfld</keyword>
          <keyword>bits.setfld</keyword>
          <keyword>bits.buf</keyword>
          <keyword>io.open</keyword>
          <keyword>io.close</keyword>
          <keyword>io.read</keyword>
          <keyword>io.write</keyword>
          <keyword>io.seek</keyword>
          <keyword>io.tell</keyword>
          <keyword>io.readln</keyword>
          <keyword>io.stat</keyword>
          <keyword>thread.newthread</keyword>
          <keyword>thread.newlock</keyword>
          <keyword>thread.lock</keyword>
          <keyword>thread.unlock</keyword>
          <keyword>thread.newsem</keyword>
          <keyword>thread.semdown</keyword>
          <keyword>thread.semup</keyword>
          <keyword>regex.comp</keyword>
          <keyword>regex.exec</keyword>
          <keyword>readline</keyword>
        </context>


        <context id="node-object" style-ref="builtin-function">
# Open VSCode
          <prefix>(?&lt;=getNode\({path}\)\.)</prefix> <!-- look behind for getNode("[path]"). as a prefix-->
# Go to File -> Preferences -> Extensions or {{key press|Ctrl|Shift|X}},
          <keyword>getType</keyword>
# Search for nasal
          <keyword>getName</keyword>
# Click install for Nasal-lang
          <keyword>getIndex</keyword>
          <keyword>getValue</keyword>
          <keyword>getParent</keyword>
          <keyword>getChild</keyword>
          <keyword>getChildren</keyword>
          <keyword>removeChild</keyword>
          <keyword>setValues</keyword>
          <keyword>setDoubleValue</keyword>
          <keyword>setIntValue</keyword>
          <keyword>setBoolValue</keyword>
          <keyword>setValue</keyword>
          <keyword>setValues</keyword>
        </context>
      </include>
    </context>
  </definitions>
</language>
</syntaxhighlight></nowiki>


If you use Mac, move it into your ''Applications/gedit/Contents/Resources/share/gtksourceview-2.0/language specs'' folder.
Manual installation:
And for linux, move it into your ''/usr/share/gtksourceview-X.0/language specs'' folder.


== Vim ==
# Download the latest release from the repository, it should be a .vsix file.
It also makes understanding other people's code easier. One such editor is the free vim or its gui variant gvim.  
# Install the extension manually by doing the command 'code --install-extension path_to_extension.vsix'


It's not for everyone but it's free, and testing it doesn't hurt: http://www.vim.org/.
{{-}}
 
The syntax definition file comes with the FlightGear code (http://gitorious.org/fg/flightgear/blobs/next/scripts/syntax/nasal.vim). Highlighting works even for Nasal embedded in XML files (type ":set ft=nasal", where ft stands for file-type)
 
Here's an example, which demonstrates a short code segment with three syntax errors as well as the highlighting of a matching pair of parentheses (yellow) and trailing spaces (blue x). (The leading blue dots aren't on by default. They help to spot tab crimes.)
 
[[File:Vim-nasal-syntax-highlighting.png|400px|thumb|Screen shot illustrating syntax highlighting in Vim]]


== Other editors ==
== Other editors ==
Nasal being syntactically very close to other programming languages like C, Php or JavaScript, you can get some usable highlighting even without real Nasal support:
Nasal being syntactically very close to other programming languages like C, Php or JavaScript, you can get some usable highlighting even without real Nasal support:


* TextMate [http://flightgear.org/forums/viewtopic.php?f=30&t=12495&p=128116&hilit=syntax+nasal#p127828]
* TextMate {{forum link|hilit=syntax+nasal|p=127828}}
* SciTe [http://flightgear.org/forums/viewtopic.php?f=30&t=9260&p=94521&hilit=syntax+nasal#p94521]
* SciTe {{forum link|hilit=syntax+nasal|p=94521}}
* gEdit (Linux) : Using the 'Octave' highlighting mode, most of the nasal functions work.


== FlightGear Wiki ==
== FlightGear Wiki ==
A common approach in code examples on the wiki is to use php syntax highlighting beween <code>&lt;syntaxhighlight&gt;</code>:
{{Note|As of 02/2016, we do not currently have a dedicated Nasal module available [http://wiki.flightgear.org/FlightGear_wiki:Village_pump/Archive_2016#Nasal_Syntaxhighlighting]}}
{{main article|Help:Formatting#Syntax highlighting}}


  <nowiki><syntaxhighlight lang="php">
Using the &lt;syntaxhighight&gt; tag with a lang="nasal" attribute, we can have highlighting right here on the wiki. Use enclose="div" to wrap the text, if it happens to be particularly wide.
 
  <nowiki><syntaxhighlight lang="nasal">
# hello.nas
# hello.nas
print('Hello World!');
print('Hello World!');
Line 443: Line 146:


Which renders into:
Which renders into:
<syntaxhighlight lang="php">
<syntaxhighlight lang="nasal">
# hello.nas
# hello.nas
print('Hello World!');
print('Hello World!');
</syntaxhighlight>
</syntaxhighlight>
== Syntax highlighting test ==
Just a collection of keywords, etc. to test whether highlighting works (you can copy this to test your own highlighting):
<syntaxhighlight lang="nasal">
# this is a comment
# operators (if applicable):
!a ? a+b - c/d*e : f~g; expr1 and expr2 or expr3;
# Builtin functions, strings
print('Hello World!');
die("We have an error, Houston!", arg[0]);
cmdarg().getNode("setting").getValue();
streq(typeof(id(keys(hash))),10);
# Loopoids
foreach (var a; ["haha", {command:"NASAL!"}, me]) {
    if(0) break;
    elsif(1) continue;
    else return;
    while(1) sprintf("%s%s\n%s=%f", "Spam", "spam", "spam", 0e-0);
    for (var i=0; i < 0.00; i += 0x0) printf("%d", int(i));
    forindex(var o; a) (func {
        var o = o;
        setlistener("/", func print(o), 1, 2);
    })();
}
# String escaping stress tests:
'\a\b\c\"\\\?' # none of these
"\e\?\'\f\a" # none of these
'\'' # this one
"\"\r\n\t\\\t" # and all of these
# And optionally string formatting:
"%s%%s%.0f%8d" # the second "s" shouldn't be highlighted, otherwise everything else
# Syntax error!:
%$@&^|\`
</syntaxhighlight>
== Related content ==
* {{forum url|p=164155}}
* {{forum url|t=12495}}
* {{forum url|t=15972}}
* {{forum url|t=9812}}


[[Category:Nasal howto]]
[[Category:Nasal howto]]
== Related ==
* http://www.flightgear.org/forums/viewtopic.php?f=30&t=17265&p=164155#p164155
* http://www.flightgear.org/forums/viewtopic.php?f=30&t=12495
* http://www.flightgear.org/forums/viewtopic.php?f=30&t=15972
* http://www.flightgear.org/forums/viewtopic.php?f=30&t=9812

Revision as of 23:53, 12 August 2019

There's Nasal syntax-highlighting support available for some editors, which is a big advantage, as it makes Nasal coding much easier.

Syntax highlighting can often point to syntax errors and so reduce the number of tedious time-consuming and unproductive FlightGear runs. It can also be a sort of "guide" to the language, highlighting functions or keywords that are builtin, so you can quickly check if it is "type" or "typeof" by typing each. In addition makes understanding other people's code easier, since you have all of the parts visually separated out, like loops, strings, constants, and builtin functions.

Atom

Script is available from Github and can also be found in the Atom's packages repository.

Manual installation:

  1. Download the latest release from https://github.com/www2000/atom-language-nasal
  2. Unpack the tar.bz2
  3. Copy/move the language-nasal package to ~/.atom/packages

For installation from atom's packages repository:

  1. Open Atom
  2. Go to Preferences Edit -> Preferences or Ctrl++,
  3. Click on packages and search for nasal
  4. Click install for Language Nasal

Emacs

Nasal syntax highlighting in GNU Emacs

There is a Emacs major mode for Nasal available here: https://github.com/andyross/nasal/blob/master/misc/nasal-mode.el

Under Linux just drop the file where you have the rest of your manually installed packages and add the following to your .emacs file:

(require 'nasal-mode)

If you don't have a directory to place your manually installed packages, put the file in the directory ~/.emacs.d/lisp/ (create it if it doesn't exist) and then put the following in your .emacs file before the require:

;; Tell emacs where is your personal elisp lib dir
(add-to-list 'load-path "~/.emacs.d/lisp/")

Reload .emacs via M-x load-file or by restarting Emacs.

gedit

Screen shot illustrating syntax highlighting in gedit

Philosopher on the FlightGear Forum has created syntax highlighting for gedit, a popular and simple text editor for Mac OS X, Linux, and Windows.

To install, copy the nasal.lang file from the first post of the forum topic [1] This is a link to the FlightGear forum. and:

  • If you use Mac, move it into your Applications/gedit/Contents/Resources/share/gtksourceview-X.0/language specs folder.
  • For Linux, move it into your /usr/share/gtksourceview-X.0/language-specs folder.

In the same post given above, there are instructions for how to edit xml.lang (in the same directory) to add the embedded XML content support (with this minor edit, Nasal highlighting will be used inside of certain tags, instead of plain text).

For "snippets" support, nasal.xml from the above post has to be moved into /usr/share/gedit/plugins/snippets/ folder or installed using the "import" feature. See this image for more details:

how to import Nasal snippets.

jEdit

There's a syntax highlighting mode for jEdit, programmer's text editor

Included are some of Nasal's internal functions and those functions currently implemented in NasalSys.cxx To use, add the content of the catalog inside your own catalog (do NOT overwrite the file) and nasal.xml in /home/USER/.jedit/modes

Restart jEdit and you can use it. Current extension is .nas, you can add your own extension and functions. This mode is heavily based on the Javascript mode.

Get it here: https://sourceforge.net/projects/fgscheduleview/files/jedit/catalog.zip/download

KDE Editors

There's a highlighting definition for nasal available at opendesktop.org (https://www.opendesktop.org/p/1187022/)

Download the file and copy it into:

~/.kde4/share/apps/katepart/syntax/ (for KDE4)
or
~/.local/share/katepart5/syntax/ (for KF5 pre 5.29)
or
~/.local/share/org.kde.syntax.highlighting/syntax/ (for current KF5 versions)

(you can create the folders if they don't exist yet).

Note that the syntax highlighting in KDE doesn't set colors, it just identifies structural elements, colors are defined by the schemas used by each editor, thus you get consistent highlighting between different languages.

The highlighting file should work with all editors based on the katepart: KWrite, Kate, and the editor component of KDevelop.

Notepad++

Screenshot of Nasal support for Notepad++

Provides comprehensive syntax highlighting and a function list parser with support for hierarchical display of both inline and out-of-body class member functions.

Alternative (syntax highlighting only):


Sublime Text 2

Sublime Text 2 syntax highlighting

Scripts and installation instructions are here: https://github.com/freevryheid/nasal

Vim

Screen shot illustrating syntax highlighting in Vim

One such editor is the free vim or its GUI variant gvim.

It's not for everyone but it's free, and testing it doesn't hurt: http://www.vim.org/.

The syntax definition file comes with the FlightGear code (flightgear/scripts/syntax/nasal.vim). Highlighting works even for Nasal embedded in XML files (type ":set ft=nasal", where ft stands for file-type)

Here's an example, which demonstrates a short code segment with three syntax errors as well as the highlighting of a matching pair of parentheses (yellow) and trailing spaces (blue x). (The leading blue dots aren't on by default. They help to spot tab crimes.)

Visual Studio Code

Screen shot illustrating syntax highlighting in VSCode

Visual Studio Code is a source-code editor developed by Microsoft for Windows, Linux and macOS. Its open source and free.

The nasal extension is available from Github and can also be found in the VSCode's marketplace. It supports basic syntax highlighting and snippets to improve development speed.

For installation from the marketplace:

  1. Open VSCode
  2. Go to File -> Preferences -> Extensions or Ctrl+ Shift+X,
  3. Search for nasal
  4. Click install for Nasal-lang

Manual installation:

  1. Download the latest release from the repository, it should be a .vsix file.
  2. Install the extension manually by doing the command 'code --install-extension path_to_extension.vsix'


Other editors

Nasal being syntactically very close to other programming languages like C, Php or JavaScript, you can get some usable highlighting even without real Nasal support:

FlightGear Wiki

Note  As of 02/2016, we do not currently have a dedicated Nasal module available [4]

1rightarrow.png See Help:Formatting#Syntax highlighting for the main article about this subject.

Using the <syntaxhighight> tag with a lang="nasal" attribute, we can have highlighting right here on the wiki. Use enclose="div" to wrap the text, if it happens to be particularly wide.

<syntaxhighlight lang="nasal">
# hello.nas
print('Hello World!');
</syntaxhighlight>

Which renders into:

# hello.nas
print('Hello World!');

Syntax highlighting test

Just a collection of keywords, etc. to test whether highlighting works (you can copy this to test your own highlighting):

# this is a comment
# operators (if applicable):
!a ? a+b - c/d*e : f~g; expr1 and expr2 or expr3;
# Builtin functions, strings
print('Hello World!');
die("We have an error, Houston!", arg[0]);
cmdarg().getNode("setting").getValue();
streq(typeof(id(keys(hash))),10);
# Loopoids
foreach (var a; ["haha", {command:"NASAL!"}, me]) {
    if(0) break;
    elsif(1) continue;
    else return;
    while(1) sprintf("%s%s\n%s=%f", "Spam", "spam", "spam", 0e-0);
    for (var i=0; i < 0.00; i += 0x0) printf("%d", int(i));
    forindex(var o; a) (func {
        var o = o;
        setlistener("/", func print(o), 1, 2);
    })();
}
# String escaping stress tests:
'\a\b\c\"\\\?' # none of these
"\e\?\'\f\a" # none of these
'\'' # this one
"\"\r\n\t\\\t" # and all of these
# And optionally string formatting:
"%s%%s%.0f%8d" # the second "s" shouldn't be highlighted, otherwise everything else
# Syntax error!:
%$@&^|\`

Related content