Skip to content

Commit

Permalink
Merge pull request #166 from Jos512/tradingview
Browse files Browse the repository at this point in the history
Add TradingView lexer
  • Loading branch information
alecthomas authored Sep 10, 2018
2 parents 5d7fef2 + fccc5bb commit d5ca12b
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ translators for Pygments lexers and styles.

## Supported languages

ABNF, ANTLR, APL, ActionScript, ActionScript 3, Ada, Angular2, ApacheConf, AppleScript, Awk, BNF, Ballerina, Base Makefile, Bash, Batchfile, BlitzBasic, Brainfuck, C, C#, C++, CFEngine3, CMake, COBOL, CSS, Cap'n Proto, Ceylon, ChaiScript, Cheetah, Clojure, CoffeeScript, Common Lisp, Coq, Crystal, Cython, DTD, Dart, Diff, Django/Jinja, Docker, EBNF, Elixir, Elm, EmacsLisp, Erlang, FSharp, Factor, Fish, Forth, Fortran, GAS, GDScript, GLSL, Genshi, Genshi HTML, Genshi Text, Gnuplot, Go, Go HTML Template, Go Text Template, Groovy, HTML, HTTP, Handlebars, Haskell, Haxe, Hexdump, Hy, INI, Idris, Io, JSON, JSX, Java, JavaScript, Julia, Kotlin, LLVM, Lighttpd configuration file, Lua, Mako, Mason, Mathematica, MiniZinc, Modula-2, MorrowindScript, MySQL, Myghty, NASM, Newspeak, Nginx configuration file, Nim, Nix, OCaml, Objective-C, Octave, Org Mode, PHP, PL/pgSQL, POVRay, PacmanConf, Perl, Pig, PkgConfig, PostScript, PostgreSQL SQL dialect, PowerShell, Prolog, Protocol Buffer, Puppet, Python, Python 3, QBasic, R, Racket, Ragel, Rexx, Ruby, Rust, SCSS, SPARQL, SQL, Sass, Scala, Scheme, Scilab, Smalltalk, Smarty, Snobol, Solidity, SquidConf, Swift, TASM, TOML, Tcl, Tcsh, TeX, Termcap, Terminfo, Terraform, Thrift, Transact-SQL, Turtle, Twig, TypeScript, TypoScript, TypoScriptCssData, TypoScriptHtmlData, VHDL, VimL, WDTE, XML, Xorg, YAML, cfstatement, markdown, reStructuredText, reg, systemverilog, verilog
ABNF, ANTLR, APL, ActionScript, ActionScript 3, Ada, Angular2, ApacheConf, AppleScript, Awk, BNF, Ballerina, Base Makefile, Bash, Batchfile, BlitzBasic, Brainfuck, C, C#, C++, CFEngine3, CMake, COBOL, CSS, Cap'n Proto, Ceylon, ChaiScript, Cheetah, Clojure, CoffeeScript, Common Lisp, Coq, Crystal, Cython, DTD, Dart, Diff, Django/Jinja, Docker, EBNF, Elixir, Elm, EmacsLisp, Erlang, FSharp, Factor, Fish, Forth, Fortran, GAS, GDScript, GLSL, Genshi, Genshi HTML, Genshi Text, Gnuplot, Go, Go HTML Template, Go Text Template, Groovy, HTML, HTTP, Handlebars, Haskell, Haxe, Hexdump, Hy, INI, Idris, Io, JSON, JSX, Java, JavaScript, Julia, Kotlin, LLVM, Lighttpd configuration file, Lua, Mako, Mason, Mathematica, MiniZinc, Modula-2, MorrowindScript, MySQL, Myghty, NASM, Newspeak, Nginx configuration file, Nim, Nix, OCaml, Objective-C, Octave, Org Mode, PHP, PL/pgSQL, POVRay, PacmanConf, Perl, Pig, PkgConfig, PostScript, PostgreSQL SQL dialect, PowerShell, Prolog, Protocol Buffer, Puppet, Python, Python 3, QBasic, R, Racket, Ragel, Rexx, Ruby, Rust, SCSS, SPARQL, SQL, Sass, Scala, Scheme, Scilab, Smalltalk, Smarty, Snobol, Solidity, SquidConf, Swift, TASM, TOML, Tcl, Tcsh, TeX, Termcap, Terminfo, Terraform, Thrift, TradingView, Transact-SQL, Turtle, Twig, TypeScript, TypoScript, TypoScriptCssData, TypoScriptHtmlData, VHDL, VimL, WDTE, XML, Xorg, YAML, cfstatement, markdown, reStructuredText, reg, systemverilog, verilog

_I will attempt to keep this section up to date, but an authoritative list can be
displayed with `chroma --list`._
Expand Down
36 changes: 36 additions & 0 deletions lexers/t/tradingview.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package t

import (
. "github.com/alecthomas/chroma" // nolint
"github.com/alecthomas/chroma/lexers/internal"
)

// TradingView lexer.
var TradingView = internal.Register(MustNewLexer(
&Config{
Name: "TradingView",
Aliases: []string{"tradingview", "tv"},
Filenames: []string{"*.tv"},
MimeTypes: []string{"text/x-tradingview"},
DotAll: true,
},
Rules{
"root": {
{`[^\S\n]+|\n|[()]`, Text, nil},
{`(//.*?)(\n)`, ByGroups(CommentSingle, Text), nil},
{`>=|<=|==|!=|>|<|\?|-|\+|\*|\/|%|\[|\]`, Operator, nil},
{`[:,.]`, Punctuation, nil},
{`=`, KeywordPseudo, nil},
{`"(\\\\|\\"|[^"\n])*["\n]`, LiteralString, nil},
{`'\\.'|'[^\\]'`, LiteralString, nil},
{`[0-9](\.[0-9]*)?([eE][+-][0-9]+)?`, LiteralNumber, nil},
{`(abs|acos|alertcondition|alma|asin|atan|atr|avg|barcolor|barssince|bgcolor|cci|ceil|change|cog|correlation|cos|crossover|crossunder|cum|dev|ema|exp|falling|fill|fixnan|floor|heikinashi|highest|highestbars|hline|iff|input|kagi|linebreak|linreg|log|log10|lowest|lowestbars|macd|max|min|mom|nz|percentile_linear_interpolation|percentile_nearest_rank|percentrank|pivothigh|pivotlow|plot|plotarrow|plotbar|plotcandle|plotchar|plotshape|pointfigure|pow|renko|rising|rma|roc|round|rsi|sar|security|sign|sin|sma|sqrt|stdev|stoch|study|sum|swma|tan|tostring|tsi|valuewhen|variance|vwma|wma|strategy\.(cancel|cancel_all|close|close_all|entry|exit|order)|strategy\.risk\.(allow_entry_in|max_cons_loss_days|max_drawdown|max_intraday_filled_orders|max_intraday_loss|max_position_size))\b`, NameFunction, nil},
{`\b(cross|dayofmonth|dayofweek|hour|minute|month|na|offset|second|tickerid|time|tr|vwap|weekofyear|year)(\()`, ByGroups(NameFunction, Text), nil}, // functions that can also be variable
{`(accdist|aqua|area|areabr|black|blue|bool|circles|close|columns|currency\.(AUD|CAD|CHF|EUR|GBP|HKD|JPY|NOK|NONE|NZD|SEK|SGD|TRY|USD|ZAR)|dashed|dotted|float|friday|fuchsia|gray|green|high|histogram|hl2|hlc3|integer|interval|isdaily|isdwm|isintraday|ismonthly|isweekly|lime|line|linebr|location\.(abovebar|belowbar|bottom|top)|low|maroon|monday|n|navy|ohlc4|olive|open|orange|period|purple|red|resolution|saturday|scale\.(left|none|right)|session|session\.(extended|regular)|silver|size\.(auto|huge|large|normal|small|tiny)|solid|source|string|sunday|symbol|syminfo\.(mintick|pointvalue|prefix|root|session)|teal|thursday|ticker|tuesday|volume|wednesday|white|yellow|strategy\.(cash|position_size|closedtrades|direction\.(all|long|short)|equity|eventrades|fixed|grossloss|grossprofit|initial_capital|long|losstrades|max_contracts_held_all|max_contracts_held_long|max_contracts_held_short|max_drawdown|netprofit|oca\.(cancel|none|reduce)|openprofit|opentrades|percent_of_equity|position_avg_price|position_entry_name|short|wintrades)|shape\.(arrowdown|arrowup|circle|cross|diamond|flag|labeldown|labelup|square|triangledown|triangleup|xcross)|barstate\.is(first|history|last|new|realtime)|barmerge\.(gaps_on|gaps_off|lookahead_on|lookahead_off)|strategy\.commission\.(cash_per_contract|cash_per_order|percent))\b`, NameVariable, nil},
{`(cross|dayofmonth|dayofweek|hour|minute|month|na|second|tickerid|time|tr|vwap|weekofyear|year)(\b[^\(])`, ByGroups(NameVariable, Text), nil}, // variables that can also be function
{`(true|false)\b`, KeywordConstant, nil},
{`(and|or|not|if|else|for)\b`, OperatorWord, nil},
{`@?[_a-zA-Z]\w*`, Text, nil},
},
},
))
12 changes: 12 additions & 0 deletions lexers/testdata/tradingview.actual
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//@version=3
study(title="Chroma example script", overlay=true, precision=3)

emaLen = input(title="EMA Length", type=integer, defval=10)
emaVal = ema(close, emaLen)

plot(series=emaVal, style=circles, offset=2, linewidth=3)

// Colour background
bgcolor(color=close > open ? orange :
close != high[1] ? purple :
na, transp=80)
93 changes: 93 additions & 0 deletions lexers/testdata/tradingview.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
[
{"type":"CommentSingle","value":"//@version=3\r"},
{"type":"Text","value":"\n"},
{"type":"NameFunction","value":"study"},
{"type":"Text","value":"(title"},
{"type":"KeywordPseudo","value":"="},
{"type":"LiteralString","value":"\"Chroma example script\""},
{"type":"Punctuation","value":","},
{"type":"Text","value":" overlay"},
{"type":"KeywordPseudo","value":"="},
{"type":"KeywordConstant","value":"true"},
{"type":"Punctuation","value":","},
{"type":"Text","value":" precision"},
{"type":"KeywordPseudo","value":"="},
{"type":"LiteralNumber","value":"3"},
{"type":"Text","value":")\r\n\r\nemaLen "},
{"type":"KeywordPseudo","value":"="},
{"type":"Text","value":" "},
{"type":"NameFunction","value":"input"},
{"type":"Text","value":"(title"},
{"type":"KeywordPseudo","value":"="},
{"type":"LiteralString","value":"\"EMA Length\""},
{"type":"Punctuation","value":","},
{"type":"Text","value":" type"},
{"type":"KeywordPseudo","value":"="},
{"type":"NameVariable","value":"integer"},
{"type":"Punctuation","value":","},
{"type":"Text","value":" defval"},
{"type":"KeywordPseudo","value":"="},
{"type":"LiteralNumber","value":"10"},
{"type":"Text","value":")\r\nemaVal "},
{"type":"KeywordPseudo","value":"="},
{"type":"Text","value":" "},
{"type":"NameFunction","value":"ema"},
{"type":"Text","value":"("},
{"type":"NameVariable","value":"close"},
{"type":"Punctuation","value":","},
{"type":"Text","value":" emaLen)\r\n\r\n"},
{"type":"NameFunction","value":"plot"},
{"type":"Text","value":"(series"},
{"type":"KeywordPseudo","value":"="},
{"type":"Text","value":"emaVal"},
{"type":"Punctuation","value":","},
{"type":"Text","value":" style"},
{"type":"KeywordPseudo","value":"="},
{"type":"NameVariable","value":"circles"},
{"type":"Punctuation","value":","},
{"type":"Text","value":" offset"},
{"type":"KeywordPseudo","value":"="},
{"type":"LiteralNumber","value":"2"},
{"type":"Punctuation","value":","},
{"type":"Text","value":" linewidth"},
{"type":"KeywordPseudo","value":"="},
{"type":"LiteralNumber","value":"3"},
{"type":"Text","value":")\r\n\r\n"},
{"type":"CommentSingle","value":"// Colour background\r"},
{"type":"Text","value":"\n"},
{"type":"NameFunction","value":"bgcolor"},
{"type":"Text","value":"(color"},
{"type":"KeywordPseudo","value":"="},
{"type":"NameVariable","value":"close"},
{"type":"Text","value":" "},
{"type":"Operator","value":"\u003e"},
{"type":"Text","value":" "},
{"type":"NameVariable","value":"open"},
{"type":"Text","value":" "},
{"type":"Operator","value":"?"},
{"type":"Text","value":" "},
{"type":"NameVariable","value":"orange"},
{"type":"Text","value":" "},
{"type":"Punctuation","value":":"},
{"type":"Text","value":"\r\n "},
{"type":"NameVariable","value":"close"},
{"type":"Text","value":" "},
{"type":"Operator","value":"!="},
{"type":"Text","value":" "},
{"type":"NameVariable","value":"high"},
{"type":"Operator","value":"["},
{"type":"LiteralNumber","value":"1"},
{"type":"Operator","value":"]"},
{"type":"Text","value":" "},
{"type":"Operator","value":"?"},
{"type":"Text","value":" "},
{"type":"NameVariable","value":"purple"},
{"type":"Text","value":" "},
{"type":"Punctuation","value":":"},
{"type":"Text","value":"\r\n "},
{"type":"NameVariable","value":"na"},
{"type":"Text","value":", transp"},
{"type":"KeywordPseudo","value":"="},
{"type":"LiteralNumber","value":"80"},
{"type":"Text","value":")"}
]

0 comments on commit d5ca12b

Please sign in to comment.