FlightGear wiki:Village pump: Difference between revisions

Jump to navigation Jump to search
→‎Nasal Syntaxhighlighting: Code for Nasal Pygments lexer
(→‎Discussion about quotes on the wiki: All articles using Instant-Cquotes can now be found in Category:Articles containing Instant-Cquotes)
(→‎Nasal Syntaxhighlighting: Code for Nasal Pygments lexer)
Line 110: Line 110:
::: [[User:Red_Leader|<span style="color:red">'''''Red Leader'''''</span>]] ([[User_talk:Red_Leader|Talk]], [[Special:Contributions/Red_Leader|contribs]]) 16:59, 23 February 2016 (EST)
::: [[User:Red_Leader|<span style="color:red">'''''Red Leader'''''</span>]] ([[User_talk:Red_Leader|Talk]], [[Special:Contributions/Red_Leader|contribs]]) 16:59, 23 February 2016 (EST)


:: Unless Gijs is facing any problems, I don't think it's necessarily needed, see my comment/suggestion in this revision: [http://wiki.flightgear.org/index.php?title=FlightGear_wiki:Village_pump&oldid=93166] [[User:Hooray|Hooray]] ([[User talk:Hooray|talk]]) 17:20, 23 February 2016 (EST)
:::: Unless Gijs is facing any problems, I don't think it's necessarily needed, see my comment/suggestion in this revision: [http://wiki.flightgear.org/index.php?title=FlightGear_wiki:Village_pump&oldid=93166] [[User:Hooray|Hooray]] ([[User talk:Hooray|talk]]) 17:20, 23 February 2016 (EST)
 
:::Hi Gijs,
:::Here's the code for a Nasal lexer. Be warned, it's thoroughly untested, but has the following features:
:::* Full support for all three string types (backtick, single quote, and double quote), including escapes and formatting strings (e.g., for {{func link|sprintf}}).
:::* All kinds of numbers, including numbers in scientific notation and octal and hex numbers.
:::* All global functions and variables as of FG v2016.1.1.
:::* Some of the commonly-used <code>props.Node</code> methods.
:::* All the other things that can be expected (keywords, punctuation, etc.).
:::I have also created a lexer based on the XML lexer for XML with embedded Nasal, which I thought would be useful.
:::Regards,
:::[[User:Red_Leader|<span style="color:red">'''''Red Leader'''''</span>]] ([[User_talk:Red_Leader|Talk]], [[Special:Contributions/Red_Leader|contribs]]) 16:35, 2 April 2016 (EDT)
 
:::http://pygments.org/docs/lexerdevelopment/#adding-and-testing-a-new-lexer
:::<syntaxhighlight lang="python" enclose="div">
# -*- coding: utf-8 -*-
"""
Lexer for Nasal.
"""
 
from pygments.lexer import RegexLexer, words, include, inherit, bygroups, using
from pygments.token import Text, Keyword, Name, String, Number, Operator, Punctuation, Comment
from pygments.lexers.html import XmlLexer
 
__all__ = ['NasalLexer', 'XMLNasalLexer']
 
class NasalLexer(RegexLexer):
 
name = 'Nasal'
aliases = ['nasal']
filenames = ['*.nas']
 
tokens = {
'formatters': [
(r'%[-#0 +]*(?:[0-9]+)?(?:\.[0-9]+)?[dis%couxXeEfFgG]', String.Interpol),
],
'backtick': [
(r'`', String.Backtick, '#pop'),
(r'[^`\\]+', String.Backtick),
(r'\\n|\\r|\\t|\\`|\\\\|\\x[0-9a-fA-F]{2}', String.Escape),
],
'sqstring': [
(r"'", String.Single, '#pop'),
(r"[^'\\%]+", String.Single),
(r"\\'", String.Escape),
include('formatters'),
],
'dqstring': [
(r'"', String.Double, '#pop'),
(r'[^"\\%]+', String.Double),
(r'\\n|\\r|\\t|\\"|\\\\|\\x[0-9a-fA-F]{2}', String.Escape),
include('formatters'),
],
'root': [
(r'\s+', Text),
(r'#.*?$'m, Comment.Single),
(r':|\?|[!=<>+\-*\/~&|^]=?', Operator),
(words(('or', 'and'), suffix=r'\b'), Operator.Word),
(r'[{(\[})\]\.;,]', Punctuation),
(words(('for', 'foreach', 'forindex', 'while', 'break', 'return', 'continue', 'if', 'else', 'elsif'), suffix=r'\b'), Keyword),
(words(('var', 'func'), suffix=r'\b'), Keyword.Declaration),
(words(('nil'), suffix=r'\b'), Keyword.Constant),
(words(('me', 'arg'), suffix=r'\b'), Name.Builtin.Pseudo),
(words(('new', 'del', 'getNode', 'getParent', 'getChild', 'getChildren', 'removeChild', 'removeChildren', 'removeAllChildren', 'getName', 'getIndex', 'getType', 'getAttribute', 'setAttribute', 'getValue', 'setValue', 'setIntValue', 'setBoolValue', 'setDoubleValue', 'unalias', 'alias', 'getPath', 'getBoolValue', 'remove', 'setValues', 'getValues', 'initNode'), suffix=r'\b'), Keyword.Pseudo),
(r'0o[0-7]+', Number.Oct),
(r'0x[0-9a-fA-F]+', Number.Hex),
(r'\d*(?:\.\d*)?[eE][+-]?\d+', Number.Float),
(r'\d*\.\d*', Number.Float),
(r'\b[0-9]+\b', Number.Integer),
(words(('D2R', 'R2D', 'FT2M', 'M2FT', 'IN2M', 'M2IN', 'NM2M', 'M2NM', 'KT2MPS', 'MPS2KT', 'FPS2KT', 'KT2FPS', 'LB2KG', 'KG2LB', 'GAL2L', 'L2GAL'), suffix=r'\b'), Name.Variable.Global),
(words(('abort', 'abs', 'addcommand', 'airportinfo', 'airwaysRoute', 'assert', 'carttogeod', 'cmdarg', 'courseAndDistance', 'createViaTo', 'createDiscontinuity', 'createWP', 'createWPFrom', 'defined', 'directory', 'fgcommand', 'findAirportsByICAO', 'findAirportsWithinRange', 'findFixesByID', 'findNavaidByFrequency', 'findNavaidsByFrequency', 'findNavaidsByID', 'findNavaidsWithinRange', 'finddata', 'flightplan', 'geodinfo', 'geodtocart', 'getprop', 'greatCircleMove', 'interpolate', 'isa', 'logprint', 'magvar', 'maketimer', 'md5', 'navinfo', 'parse_markdown', 'parsexml', 'print', 'printf', 'printlog', 'rand', 'registerFlightPlanDelegate', 'removecommand', 'removelistener', 'resolvepath', 'setlistener', 'setprop', 'settimer', 'srand', 'systime', 'thisfunc', 'tileIndex', 'tilePath', 'values'), suffix=r'\b'), Name.Builtin),
(words(('append', 'bind', 'call', 'caller', 'chr', 'closure', 'cmp', 'compile', 'contains', 'delete', 'die', 'find', 'ghosttype', 'id', 'int', 'keys', 'left', 'num', 'pop', 'right', 'setsize', 'size', 'sort', 'split', 'sprintf', 'streq', 'substr', 'subvec', 'typeof'), suffix=r'\b'), Name.Builtin),
(words(('_createCondition', '_fgcommand', '_interpolate', '_setlistener'), suffix=r'\b'), Keyword.Reserved),
(r'`', String.Backtick, 'backtick'),
(r"'", String.Single, 'sqstring'),
(r'"', String.Double 'dqstring'),
(r'\b_\w*?\b', Keyword.Reserved),
#(r'\b\w*?\b', Name),
]
}
 
class XMLNasalLexer(XmlLexer):
"""
For Nasal code embedded in XML files.
"""
 
name = 'XML-Nasal'
aliases = ['xml-nasal', 'xml-ns']
 
tokens = {
'root': [
(r'(<(?:load|unload|script)>)(<!\[CDATA\[)?(.*?)(]]>)?(</(?:load|unload|script)>)', bygroups(Name.Tag, Comment.Preproc, using(NasalLexer), Comment.Preproc, Name.Tag),
inherit,
],
}
</syntaxhighlight>


:: We can use the ECMAScript/JavaScript lexer[http://pygments.org/docs/lexers/#lexers-for-javascript-and-related-languages] for now, my suggestion would be to copy that over to a file so that we can work on a custom Nasal lexer (Syntax  is almost identical, with a few different keywords, and many others being irrelevant). What is missing/different can be obtained from other lexers that are similar, e.g. [http://pygments.org/docs/lexers/#lexers-for-other-c-like-languages] [[User:Hooray|Hooray]] ([[User talk:Hooray|talk]]) 15:45, 19 February 2016 (EST)
:: We can use the ECMAScript/JavaScript lexer[http://pygments.org/docs/lexers/#lexers-for-javascript-and-related-languages] for now, my suggestion would be to copy that over to a file so that we can work on a custom Nasal lexer (Syntax  is almost identical, with a few different keywords, and many others being irrelevant). What is missing/different can be obtained from other lexers that are similar, e.g. [http://pygments.org/docs/lexers/#lexers-for-other-c-like-languages] [[User:Hooray|Hooray]] ([[User talk:Hooray|talk]]) 15:45, 19 February 2016 (EST)

Navigation menu