diff options
author | Greg de Lima <8351200+GD-L@users.noreply.github.com> | 2023-02-13 11:12:01 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-02-13 17:12:01 +0100 |
commit | 4ae9e07ddc8ca344f27c2d56b060fda66d5f2972 (patch) | |
tree | b2b5e9d6d8a60c099519bfde20b462a9190a2285 | |
parent | beed6143ec0587e7c6a481ffcf797f07219964f1 (diff) | |
download | pygments-git-4ae9e07ddc8ca344f27c2d56b060fda66d5f2972.tar.gz |
Add new lexer for DAX (#2335)
Co-authored-by: Jean Abou-Samra <jean@abou-samra.fr>
-rw-r--r-- | pygments/lexers/_mapping.py | 1 | ||||
-rw-r--r-- | pygments/lexers/dax.py | 113 | ||||
-rw-r--r-- | tests/examplefiles/dax/dax_example1.dax | 11 | ||||
-rw-r--r-- | tests/examplefiles/dax/dax_example1.dax.output | 45 |
4 files changed, 170 insertions, 0 deletions
diff --git a/pygments/lexers/_mapping.py b/pygments/lexers/_mapping.py index d2d7880f..ccc1b84d 100644 --- a/pygments/lexers/_mapping.py +++ b/pygments/lexers/_mapping.py @@ -121,6 +121,7 @@ LEXERS = { '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',)), diff --git a/pygments/lexers/dax.py b/pygments/lexers/dax.py new file mode 100644 index 00000000..34b20d35 --- /dev/null +++ b/pygments/lexers/dax.py @@ -0,0 +1,113 @@ +""" + pygments.lexers.dax + ~~~~~~~~~~~~~~~~~~~ + + Lexer for LilyPond. + + :copyright: Copyright 2006-2022 by the Pygments team, see AUTHORS. + :license: BSD, see LICENSE for details. +""" + +from pygments.lexer import RegexLexer, words +from pygments.token import Comment, Punctuation, Whitespace,\ + Name, Operator, String, Number, Text + +__all__ = ['DaxLexer'] + +class DaxLexer(RegexLexer): + """ + Lexer for Power BI DAX + Referenced from: https://github.com/sql-bi/SyntaxHighlighterBrushDax + + """ + name = 'Dax' + aliases = ['dax'] + filenames = ['*.dax'] + url = 'https://learn.microsoft.com/en-us/dax/dax-function-reference' + mimetypes = [] + + tokens = { + 'root': [ + (r'\s+', Whitespace), + (r"--.*\n?", Comment.Single), # Comment: Double dash comment + (r"//.*\n?", Comment.Single), # Comment: Double backslash comment + (r'/\*', Comment.Multiline, 'multiline-comments'), + (words(('abs','acos','acosh','acot','acoth','addcolumns','addmissingitems','all', + 'allcrossfiltered','allexcept','allnoblankrow','allselected','and', + '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'), + prefix=r'(?i)', suffix=r'\b'), Name.Function), #Functions + + (words(('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'), + prefix=r'(?i)', suffix=r'\b'), Name.Builtin), # Keyword + + (r':=|[-+*\/=^]', Operator), + (r'\b(IN|NOT)\b', Operator.Word), + (r'"', String, 'string'), #StringLiteral + (r"'(?:[^']|'')*'(?!')(?:\[[ \w]+\])?|\w+\[[ \w]+\]", + Name.Attribute), # Column reference + (r"\[[ \w]+\]", Name.Attribute), #Measure reference + (r'(?<!\w)(\d+\.?\d*|\.\d+\b)', Number),# Number + (r'[\[\](){}`,.]', Punctuation), #Parenthesis + (r'.*\n', Text), + + ], + 'multiline-comments': [ + (r'/\*', Comment.Multiline, 'multiline-comments'), + (r'\*/', Comment.Multiline, '#pop'), + (r'[^/*]+', Comment.Multiline), + (r'[/*]', Comment.Multiline) + ], + 'string': [ + (r'""', String.Escape), + (r'"', String, '#pop'), + (r'[^"]+', String), + ] + } diff --git a/tests/examplefiles/dax/dax_example1.dax b/tests/examplefiles/dax/dax_example1.dax new file mode 100644 index 00000000..39b48891 --- /dev/null +++ b/tests/examplefiles/dax/dax_example1.dax @@ -0,0 +1,11 @@ +/* This calculation sums sales divided +by days in the month +multiplied by current MTD +*/ +Total Sales Spread = ( + --This line does a calculate + CALCULATE ( + SUM ( TABLE1[SALES]), + PBCS_PLAN_FCST[Scenario] = "Plan" + ) / 'Days in Month'[Days in Month Value] +) * 'Current MTD'[Current MTD Value]
\ No newline at end of file diff --git a/tests/examplefiles/dax/dax_example1.dax.output b/tests/examplefiles/dax/dax_example1.dax.output new file mode 100644 index 00000000..107cde5a --- /dev/null +++ b/tests/examplefiles/dax/dax_example1.dax.output @@ -0,0 +1,45 @@ +'/*' Comment.Multiline +' This calculation sums sales divided\nby days in the month \nmultiplied by current MTD\n' Comment.Multiline + +'*/' Comment.Multiline +'\n' Text.Whitespace + +'Total Sales Spread = (\n' Text + +' ' Text.Whitespace +'--This line does a calculate\n' Comment.Single + +' ' Text.Whitespace +'CALCULATE' Name.Function +' ' Text.Whitespace +'(' Punctuation +'\n ' Text.Whitespace +'SUM' Name.Function +' ' Text.Whitespace +'(' Punctuation +' ' Text.Whitespace +'TABLE1[SALES]' Name.Attribute +')' Punctuation +',' Punctuation +'\n ' Text.Whitespace +'PBCS_PLAN_FCST[Scenario]' Name.Attribute +' ' Text.Whitespace +'=' Operator +' ' Text.Whitespace +'"' Literal.String +'Plan' Literal.String +'"' Literal.String +'\n ' Text.Whitespace +')' Punctuation +' ' Text.Whitespace +'/' Operator +' ' Text.Whitespace +"'Days in Month'[Days in Month Value]" Name.Attribute +'\n' Text.Whitespace + +')' Punctuation +' ' Text.Whitespace +'*' Operator +' ' Text.Whitespace +"'Current MTD'[Current MTD Value]" Name.Attribute +'\n' Text.Whitespace |