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

Operater optimization #26

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
201 changes: 154 additions & 47 deletions WolframLanguage.sublime-syntax
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ file_extensions:
- m
- wl
- wls
# FIXME: special process when rendering .nb file
- nb

first_line_match: ^#!.*\b(math|wolfram).*$
Expand All @@ -19,8 +18,9 @@ scope: source.wolfram
# Partly based on https://github.com/JuliaEditorSupport/Julia-sublime/blob/master/Julia.sublime-syntax

variables:
number: (?:\d+\.?|\.\d)\d*
symbol: '[$[:alpha:]]+[$[:alnum:]]*'
identifier: (?:{{symbol}}`)*({{symbol}})
identifier: (?:{{symbol}}`)*{{symbol}}

# Built-in functions
built_in_functions: |-
Expand All @@ -47,16 +47,33 @@ variables:
Catalan|Degree|E|EulerGamma|Glaisher|GoldenAngle|GoldenRatio|I|Khinchin|MachinePrecision|Pi
)\b

first_param_functional_functions: |-
(?x)
Apply|ArrayFilter|
BlockMap|
FixedPoint|FixedPointList|Fold|FoldList|FoldPair|FoldPairList|
Map|MapAt|MapIndexed|MapThread|
Nest|NestList|NestGraph|NestWhile|NestWhileList|
Scan|SequenceFold|SequenceFoldList

last_param_functional_functions: |-
(?x)
CountDistinctBy|LengthWhile|TakeWhile

parametic_functions: Table|Do|Sum|Product
shigma marked this conversation as resolved.
Show resolved Hide resolved

scoping_functions: Block|Module|With|Function

nb_code_styles: (Input)


contexts:

main:
- include: declarations
- include: expressions

expressions:
- include: declarations
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am no sure we should index all the localized functions.

Copy link
Collaborator Author

@shigma shigma Nov 15, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does "index" mean?

From my perspective, "declarations" have no grammatical difference with other expressions and should not be ruled out.

- include: pattern
- include: expressions-nopattern

Expand All @@ -66,13 +83,12 @@ contexts:
- include: shorthand
- include: operators
- include: pattern-short
- include: scoping
- include: function
- include: variable
- include: nested

comments:
- match: '(\(\*\s*)(::)([[:alnum:]]+)(::)(\s*\*\))'
- match: (\(\*\s*)(::)([[:alnum:]]+)(::)(\s*\*\))
captures:
1: comment.line.wolfram
2: constant.numeric.line-number.find-in-files
Expand All @@ -90,14 +106,19 @@ contexts:
pop: true

literals:
- match: (?:\d+\.?|\.\d)\d*\`{0,2}
# numbers, with base, scientific notation, precision or accuracy
- match: ([0-9]+\^\^)?{{number}}(\`\`?({{number}})?)?(\*\^{{number}})?
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The first {{number}} can contain letters to represent digits in bases higher than 10.
I was working on this syntax but we can do it here as well. This would be my proposal

Suggested change
- match: ([0-9]+\^\^)?{{number}}(\`\`?({{number}})?)?(\*\^{{number}})?
- match: |-
(?x)
(?:(\d+)(\^\^))
((?:[$[:alnum:]]+\.?|\.[$[:alnum:]])[$[:alnum:]]*)
(?:(\`\`?)({{number}})?)?
(?:(\*\^)([+-]?\d+))?
captures:
1: constant.numeric.base.wolfram
2: keyword.operator.base.wolfram
3: constant.numeric.wolfram
4: keyword.operator.precision.wolfram
5: constant.numeric.precision.wolfram
6: keyword.operator.exponent.wolfram
7: constant.numeric.exponent.wolfram
- match: |-
(?x)
({{number}})
(?:(\`\`?)({{number}})?)?
(?:(\*\^)([+-]?\d+))?
captures:
1: constant.numeric.wolfram
2: keyword.operator.precision.wolfram
3: constant.numeric.precision.wolfram
4: keyword.operator.exponent.wolfram
5: constant.numeric.exponent.wolfram

I know it does not distinguish between precision and accuracy but I did not want to explode the pattern even more.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And exponents can have a decimal point.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

((?:[$[:alnum:]]+.?|.[$[:alnum:]])[$[:alnum:]]*)

What is $ for?

Copy link
Collaborator Author

@shigma shigma Nov 15, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, I don't like the idea to treat ^^ and *^ as "operators". If they were operators, they should have precedence and expressions like 8 ^^ 10 should be right. But obviously not.

scope: constant.numeric.wolfram

# built-in symbols
- match: ((?:System`)?{{built_in_numeric_constants}})
scope: constant.numeric.wolfram
- match: ((?:System`)?{{built_in_constants}})
scope: constant.language.wolfram
- match: ((?:System`)?{{built_in_options}})
scope: variable.function.wolfram
scope: variable.parameter.wolfram
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know this is conceptually wrong but I think function was there to have the same colouring for all System symbols.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But built-in-constants are displayed as "constants".


# strings
- match: \"
scope: punctuation.definition.string.begin
push:
Expand All @@ -115,15 +136,33 @@ contexts:
scope: constant.other.placeholder.wolfram

shorthand:
# Out
- match: '%(\d*|%*)'
scope: storage.type.Out.wolfram

# MessageName
- match: (::)\s*([[:alnum:]]+)
captures:
1: keyword.operator.MessageName.wolfram
2: string.unquoted.wolfram
# ENHANCEMENT: infix
- match: (//)\s*({{identifier}})

# Infix
- match: ~
scope: keyword.operator.call.wolfram
push:
- meta_content_scope: meta.infix.wolfram
- match: ({{identifier}})?\s*(~)
captures:
1: variable.function.wolfram
2: keyword.operator.call.wolfram
pop: true
- include: expressions

# Postfix and compound functions
- match: (//|[@/]\*)\s*({{identifier}})
captures:
1: keyword.operator.Postfix.wolfram
3: variable.function.wolfram
1: keyword.operator.call.wolfram
2: variable.function.wolfram

operators:
- match: (@\*|@{1,3}|\/[@*])
Expand All @@ -136,28 +175,30 @@ contexts:
scope: keyword.operator.logical.wolfram
- match: (:?=|\/:|[-:]>|[+\-*/]=|<->)
scope: keyword.operator.assignment.wolfram
- match: '\/;'
- match: \/;
scope: keyword.operator.condition.wolfram
- match: (\+\+|--|[+\-*/])
- match: (\+\+|--|[+\-*/^])
scope: keyword.operator.arithmetic.wolfram
- match: (>>>?|<<<?)
scope: keyword.operator.flow.wolfram
- match: (<>|~~)
scope: keyword.operator.string.wolfram
- match: \.\.\.?
scope: keyword.operator.repeat.wolfram
- match: ';;'
- match: ;;
scope: keyword.operator.span.wolfram
- match: ';'
- match: ;
scope: keyword.operator.compound.wolfram
- match: \\\[[[:alnum:]]+\]
scope: keyword.operator.word.wolfram
- match: '&'
scope: keyword.operator.function.wolfram
scope: keyword.operator.function variable.function.wolfram
- match: \?\??
scope: keyword.operator.definition.wolfram

pattern-short:
# Including Blank, BlankSequence, BlankNullSequence
# Can also be matched with Pattern, PatternTest, Optional
# support Blank, BlankSequence, BlankNullSequence
# can also be matched with Pattern, PatternTest, Optional
- match: ({{identifier}})?(_{1,3})(({{identifier}})?)
scope: variable.parameter.wolfram
push:
Expand All @@ -174,70 +215,108 @@ contexts:
pop: true

pattern:
- match: '({{identifier}})\s*(:(?=[^:>=]))'
- match: ({{identifier}})\s*(:(?=[^:>=]))
captures:
1: variable.parameter.wolfram
3: keyword.operator.Pattern.wolfram
push:
- meta_content_scope: meta.pattern.wolfram
- match: \?
scope: keyword.operator.PatternTest.wolfram
- match: ':(?=[^:>=])'
- match: :(?=[^:>=])
scope: keyword.operator.Optional.wolfram
pop: true
# we should check the precedence here
- match: (?=[:-]>|:?=|\/[;.\/]|[\]\)\},;&]|>>|<<)
pop: true
- include: expressions-nopattern

function:
- match: ({{identifier}})\s*(\[(?!\[))
captures:
1: variable.function.wolfram
3: punctuation.section.brackets.begin.wolfram
push:
- meta_scope: meta.block.wolfram
- match: \]
scope: punctuation.section.brackets.end.wolfram
pop: true
- include: expressions
- match: ({{identifier}})(?=\s*(@\*|@{1,3}|\/[@*]))
scope: variable.function.wolfram

variable:
- match: '(?:System`)?({{built_in_functions}})'
- match: (?:System`)?({{built_in_functions}})
scope: variable.function
- match: '{{identifier}}'
scope: variable.other
- match: (#[a-zA-Z][[:alnum:]]*|#\d*)
scope: variable.parameter

scoping:
- match: (Block|Module|With|Function)\s*(\[(?!\[))
function:
# parametic functions
- match: ({{parametic_functions}})\s*(\[(?!\[))
captures:
1: variable.function.parametic.wolfram
2: punctuation.section.brackets.begin.wolfram
push:
- meta_content_scope: meta.block.wolfram
- match: ','
scope: punctuation.separator.sequence.wolfram
push: parameter
- include: meta-block

# scoping functions
- match: ({{scoping_functions}})\s*(\[(?!\[))
captures:
1: variable.function.scoping.wolfram
2: punctuation.section.brackets.begin.wolfram
push:
- meta_content_scope: meta.block.wolfram
- match: '\{'
- match: \{
scope: punctuation.section.braces.begin.wolfram
set: [localized, local]
- match: '\]'
scope: punctuation.section.brackets.end.wolfram
- include: meta-block

# functional functions
shigma marked this conversation as resolved.
Show resolved Hide resolved
- match: ({{first_param_functional_functions}})\s*(\[)\s*({{identifier}})
captures:
1: variable.function.scoping.wolfram
2: punctuation.section.brackets.begin.wolfram
3: variable.function.wolfram
push: meta-block
- match: ({{last_param_functional_functions}})\s*(\[(?!\[))
captures:
1: variable.function.scoping.wolfram
2: punctuation.section.brackets.begin.wolfram
push:
- meta_content_scope: meta.block.wolfram
- match: ({{identifier}})\s*\]
captures:
1: variable.function.wolfram
2: punctuation.section.brackets.end.wolfram
pop: true
- include: expressions
- include: meta-block

# general functions
- match: ({{identifier}})\s*(\[(?!\[))
captures:
1: variable.function.wolfram
3: punctuation.section.brackets.begin.wolfram
push: meta-block
- match: ({{identifier}})\s*(?=@{1,3}|//?@)
scope: variable.function.wolfram

parameter:
- match: (\{)\s*({{symbol}})
captures:
1: punctuation.section.braces.begin.wolfram
2: variable.parameter.wolfram
push: meta-parameter
- match: (?=\S)
pop: true

localized:
- meta_content_scope: meta.block.local.wolfram
- match: '\,'
- match: ','
scope: punctuation.separator.sequence.wolfram
push: local
- match: '\}'
- match: \}
scope: punctuation.section.braces.end.wolfram
set:
- meta_content_scope: meta.block.wolfram
- match: '\]'
- match: \]
scope: punctuation.section.brackets.end.wolfram
pop: true
- include: expressions
- match: '\]'
- match: \]
scope: punctuation.section.brackets.end.wolfram
pop: true
- include: expressions

Expand Down Expand Up @@ -278,8 +357,22 @@ contexts:
- include: sequence
- match: (?=[^\[])
pop: true
- match: ((?:{{symbol}}`)*(\${{symbol}}))(?=\s*:?=)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This rule was here to add to the index global variable declarations

$GlobalValue = xxx;

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But this rule was not grammatical. System global varibles are defined by "built-in" variables. General varibles begin with "$" are not necessarily a "constant".

scope: entity.name.constant.wolfram
# - match: ((?:{{symbol}}`)*(\${{symbol}}))(?=\s*:?=)
# scope: entity.name.constant.wolfram
- match: |-
(?x)
{{identifier}}
(?=
\s*:?=\s*
(
[^\(\)]+|
\((?>\g<-1>)*\)
)*
&(;|$)
)
scope: entity.name.function.wolfram
- match: ({{identifier}})(?=\s*:?=\s*Function\s*\[(?!\s*\[))
scope: entity.name.function.wolfram

sequence:
- meta_scope: meta.sequence.wolfram
Expand Down Expand Up @@ -330,3 +423,17 @@ contexts:
scope: punctuation.section.association.end.wolfram
pop: true
- include: expressions

meta-block:
- meta_scope: meta.block.wolfram
- match: \]
scope: punctuation.section.brackets.end.wolfram
pop: true
- include: expressions

meta-parameter:
- meta_scope: meta.parameter.wolfram
- match: \}
scope: punctuation.section.braces.end.wolfram
pop: true
- include: expressions
Loading