Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new lexer for DAX #2335

Merged
merged 10 commits into from Feb 13, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
59 changes: 59 additions & 0 deletions pygments/lexers/_dax_builtins.py
@@ -0,0 +1,59 @@
"""
pygments.lexers._dax_builtins
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"""

FUNCTIONS = {
'abs','acos','acosh','acot','acoth','addcolumns','addmissingitems','all'
,'allcrossfiltered','allexcept','allnoblankrow','allselected','and'
GD-L marked this conversation as resolved.
Show resolved Hide resolved
,'approximatedistinctcount','asin','asinh','atan','atanh','average'
,'averagea','averagex','beta.dist','beta.inv','blank','calculate'
,'calculatetable','calendar','calendarauto','ceiling','chisq.dist'
,'chisq.dist.rt','chisq.inv','chisq.inv.rt','closingbalancemonth'
,'closingbalancequarter','closingbalanceyear','coalesce','combin'
,'combina','combinevalues','concatenate','concatenatex','confidence.norm'
,'confidence.t','contains','containsrow','containsstring','containsstringexact'
,'convert','cos','cosh','cot','coth','count','counta','countax','countblank'
,'countrows','countx','crossfilter','crossjoin','currency','currentgroup'
,'customdata','datatable','date','dateadd','datediff','datesbetween'
,'datesinperiod','datesmtd','datesqtd','datesytd','datevalue','day','degrees'
,'detailrows','distinct','distinctcount','distinctcountnoblank','divide'
,'earlier','earliest','edate','endofmonth','endofquarter','endofyear'
,'eomonth','error','even','exact','except','exp','expon.dist','fact'
,'false','filter','filters','find','firstdate','firstnonblank','firstnonblankvalue'
,'fixed','floor','format','gcd','generate','generateall','generateseries','geomean'
,'geomeanx','groupby','hasonefilter','hasonevalue','hour','if','if.eager','iferror'
,'ignore','int','intersect','isblank','iscrossfiltered','isempty','iserror','iseven'
,'isfiltered','isinscope','islogical','isnontext','isnumber','iso.ceiling','isodd'
,'isonorafter','isselectedmeasure','issubtotal','istext','keepfilters',
'keywordmatch','lastdate','lastnonblank','lastnonblankvalue','lcm','left'
,'len','ln','log','log10','lookupvalue','lower','max','maxa','maxx',
'median','medianx','mid','min','mina','minute','minx','mod','month'
,'mround','naturalinnerjoin','naturalleftouterjoin'
,'nextday','nextmonth','nextquarter','nextyear','nonvisual','norm.dist','norm.inv'
,'norm.s.dist','norm.s.inv','not','now','odd','openingbalancemonth'
,'openingbalancequarter','openingbalanceyear','or','parallelperiod'
,'path','pathcontains','pathitem','pathitemreverse','pathlength','percentile.exc'
,'percentile.inc','percentilex.exc','percentilex.inc','permut','pi'
,'poisson.dist','power','previousday','previousmonth','previousquarter'
'previousyear','product','productx','quarter','quotient','radians'
,'rand','randbetween','rank.eq','rankx','related','relatedtable','removefilters'
,'replace','rept','right','rollup','rollupaddissubtotal','rollupgroup'
,'rollupissubtotal','round','rounddown','roundup','row','sameperiodlastyear'
,'sample','search','second','selectcolumns','selectedmeasure'
,'selectedmeasureformatstring','selectedmeasurename','selectedvalue'
,'sign','sin','sinh','sqrt','sqrtpi','startofmonth','startofquarter'
,'startofyear','stdev.p','stdev.s''stdevx.p','stdevx.s','substitute'
,'substitutewithindex','sum','summarize','summarizecolumns','sumx'
,'switch','t.dist','t.dist.2t','t.dist.rt','t.inv','t.inv.2t','tan'
,'tanh','time','timevalue','today','topn','topnskip','totalmtd'
,'totalqtd','totalytd','treatas','trim','true','trunc','unichar','unicode'
,'union','upper','userelationship','username','userobjectid','userprincipalname'
,'utcnow','utctoday','value','values','var.p','var.s','varx.p','varx.s'
,'weekday','weeknum','xirr','xnpv','year','yearfrac'}

KEYWORDS = {'at','asc','boolean','both','by','create','currency',
'datetime','day','define','desc','double',
'evaluate','false','integer','measure',
'month','none','order','return','single','start','string'
,'table','true','var','year'}
1 change: 1 addition & 0 deletions pygments/lexers/_mapping.py
Expand Up @@ -121,6 +121,7 @@
'DarcsPatchLexer': ('pygments.lexers.diff', 'Darcs Patch', ('dpatch',), ('*.dpatch', '*.darcspatch'), ()),
'DartLexer': ('pygments.lexers.javascript', 'Dart', ('dart',), ('*.dart',), ('text/x-dart',)),
'Dasm16Lexer': ('pygments.lexers.asm', 'DASM16', ('dasm16',), ('*.dasm16', '*.dasm'), ('text/x-dasm16',)),
'DaxLexer': ('pygments.lexers.dax', 'Dax', ('dax',), ('*.dax',), ()),
'DebianControlLexer': ('pygments.lexers.installers', 'Debian Control file', ('debcontrol', 'control'), ('control',), ()),
'DelphiLexer': ('pygments.lexers.pascal', 'Delphi', ('delphi', 'pas', 'pascal', 'objectpascal'), ('*.pas', '*.dpr'), ('text/x-pascal',)),
'DevicetreeLexer': ('pygments.lexers.devicetree', 'Devicetree', ('devicetree', 'dts'), ('*.dts', '*.dtsi'), ('text/x-c',)),
Expand Down
43 changes: 43 additions & 0 deletions pygments/lexers/dax.py
@@ -0,0 +1,43 @@
from pygments.lexer import RegexLexer, words
from pygments.token import Comment, Punctuation, Whitespace,\
Name, Operator, String, Number
from pygments.lexers._dax_builtins import FUNCTIONS, KEYWORDS

__all__ = ['DaxLexer']

class DaxLexer(RegexLexer):
"""
Lexer for Power BI DAX
Referenced from: https://github.com/sql-bi/SyntaxHighlighterBrushDax
"""
name = 'Dax'
aliases = ['dax']
filenames = ['*.dax']

GD-L marked this conversation as resolved.
Show resolved Hide resolved
functions_upper = [x.upper() for x in FUNCTIONS]
keywords_upper = [x.upper() for x in KEYWORDS]

tokens = {
'root': [
(r'\s+', Whitespace),
(r"--.*\n?", Comment.Single), # Comment: #39A03B, Double dash comment
GD-L marked this conversation as resolved.
Show resolved Hide resolved
(r"//.*\n?", Comment.Single), # Comment: #39A03B Double backslash comment
(r"/\*", Comment.Multiline, 'multiline-comments'),
(words(FUNCTIONS, prefix=r'(?i), suffix=r'\b'), Name.Function), #Functions
(words(KEYWORDS , prefix=r'(?i)', suffix=r'\b'), Name.Builtin), # Keyword
(r':=|[-+*\/=^]', Operator),
(r'\b(IN|NOT)\b', Operator.Word),
(r'\"(?:[^\"]|\"\")*\"(?!\")', String.Single), #StringLiteral
GD-L marked this conversation as resolved.
Show resolved Hide resolved
(r"'(?:[^']|'')*'(?!')(?:\[[ \w\xA0-\uFFFF]+\])?|\w+\[[ \w\xA0-\uFFFF]+\]"
GD-L marked this conversation as resolved.
Show resolved Hide resolved
, Name.Attribute), # Column reference
(r"\[[ \w\xA0-\uFFFF]+\]", Name.Attribute), #Measure reference
(r'\b\d+\.?\d*|\B\.\d+\b', Number),# Number
jeanas marked this conversation as resolved.
Show resolved Hide resolved
(r'[\[\](){}`,.]', Punctuation), #Parenthesis
],
'multiline-comments': [
(r'/\*', Comment.Multiline, '#push'),
(r'\*/', Comment.Multiline, '#pop'),
(r'[^/*]+', Comment.Multiline),
(r'[/*]', Comment.Multiline)
]
}