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

Added GSQL lexer #1809

Merged
merged 3 commits into from Jul 18, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions pygments/lexers/_mapping.py
Expand Up @@ -175,6 +175,7 @@
'GAPLexer': ('pygments.lexers.algebra', 'GAP', ('gap',), ('*.g', '*.gd', '*.gi', '*.gap'), ()),
'GDScriptLexer': ('pygments.lexers.gdscript', 'GDScript', ('gdscript', 'gd'), ('*.gd',), ('text/x-gdscript', 'application/x-gdscript')),
'GLShaderLexer': ('pygments.lexers.graphics', 'GLSL', ('glsl',), ('*.vert', '*.frag', '*.geo'), ('text/x-glslsrc',)),
'GSQLLexer': ('pygments.lexers.gsql', 'GSQL', ('gsql',), ('*.gsql',), ()),
'GasLexer': ('pygments.lexers.asm', 'GAS', ('gas', 'asm'), ('*.s', '*.S'), ('text/x-gas',)),
'GcodeLexer': ('pygments.lexers.gcodelexer', 'g-code', ('gcode',), ('*.gcode',), ()),
'GenshiLexer': ('pygments.lexers.templates', 'Genshi', ('genshi', 'kid', 'xml+genshi', 'xml+kid'), ('*.kid',), ('application/x-genshi', 'application/x-kid')),
Expand Down
78 changes: 78 additions & 0 deletions pygments/lexers/gsql.py
@@ -0,0 +1,78 @@
import re

from pygments.lexer import RegexLexer, include, bygroups, using, this
from pygments.token import Keyword, Punctuation, Comment, Operator, Name,\
String, Number, Whitespace


__all__ = ["GSQLLexer"]

class GSQLLexer(RegexLexer):
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please add a comment describing this lexer -- ideally with a link to the language definition. This comment should end with .. versionadded:: 2.10

Choose a reason for hiding this comment

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

@DanBarkus thanks for the great work 😄
@Anteru I can't wait to see this PR Merged 😄

Choose a reason for hiding this comment

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

@DanBarkus Very cool! Can't wait to use it!
@Anteru Looking forward to the merge!

name = 'GSQL'
aliases = ['gsql']
filenames = ['*.gsql']

flags = re.MULTILINE | re.IGNORECASE

tokens = {
'root': [
include('comment'),
include('keywords'),
include('clauses'),
include('accums'),
include('relations'),
include('strings'),
include('whitespace'),
include('barewords'),
include('operators'),
],
'comment': [
(r'.*\#.*\n', Comment.Single),
(r'.*\/\*\s*.*\s*\*\/', Comment.Multiline),
],
'keywords': [
(r'(ACCUM|AND|ANY|API|AS|ASC|AVG|BAG|BATCH|BETWEEN|BOOL|BOTH|'
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please use the words function for long lists of keywords which can optimize the matching, see for instance: https://github.com/pygments/pygments/blob/master/pygments/lexers/python.py#L202

r'BREAK|BY|CASE|CATCH|COALESCE|COMPRESS|CONTINUE|COUNT|'
r'CREATE|DATETIME|DATETIME_ADD|DATETIME_SUB|DELETE|DESC|DISTRIBUTED|DO|'
r'DOUBLE|EDGE|ELSE|END|ESCAPE|EXCEPTION|FALSE|FILE|FILTER|FLOAT|FOREACH|FOR|'
r'FROM|GRAPH|GROUP|GSQL_INT_MAX|GSQL_INT_MIN|GSQL_UINT_MAX|HAVING|IF|'
r'IN|INSERT|INT|INTERPRET|INTERSECT|INTERVAL|INTO|IS|ISEMPTY|JSONARRAY|JSONOBJECT|LASTHOP|'
r'LEADING|LIKE|LIMIT|LIST|LOAD_ACCUM|LOG|MAP|MATCH|MAX|MIN|MINUS|NOT|'
r'NOW|NULL|OFFSET|OR|ORDER|PATH|PER|PINNED|POST_ACCUM|POST-ACCUM|PRIMARY_ID|PRINT|'
r'QUERY|RAISE|RANGE|REPLACE|RESET_COLLECTION_ACCUM|RETURN|RETURNS|RUN|SAMPLE|SELECT|SELECT_VERTEX|'
r'SET|SRC|STATIC|STRING|SUM|SYNTAX|TARGET|TAGSTGT|THEN|TO|TO_CSV|TO_DATETIME|TRAILING|TRIM|TRUE|'
r'TRY|TUPLE|TYPEDEF|UINT|UNION|UPDATE|VALUES|VERTEX|WHEN|WHERE|WHILE|WITH)\b', Keyword),
],
'clauses': [
(r'(accum|having|limit|order|postAccum|sample|where)\b', Keyword),
],
'accums': [
(r'(andaccum|arrayaccum|avgaccum|bagaccum|bitwiseandaccum|'
r'bitwiseoraccum|groupbyaccum|heapaccum|listaccum|MapAccum|'
r'maxaccum|minaccum|oraccum|setaccum|sumaccum)\b', Keyword),
],
# 'name': [
# (r'(\@\@w+)\b', Name),
# ],
Comment on lines +61 to +63
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can you please remove this?

'relations': [
(r'(-\s?)(\(.*\:\w?\))(\s?-)', bygroups(Operator, using(this), Operator)),
(r'->|<-', Operator),
(r'[.*{}]', Punctuation),
],
'strings': [
(r'"(?:\\[tbnrf\'"\\]|[^\\"])*"', String),
(r'@{1,2}\w+', Name.Variable),
(r'(\<\w+)?\<(\w+\>?\,?\s?)+\>+', Name.Constant),
],
'whitespace': [
(r'\s+', Whitespace),
],
'barewords': [
(r'[a-z]\w*', Name),
(r'(\d+\.\d+|\d+)', Number),
],
'operators': [
(r'[^0-9|\/|\-](\-\=|\+\=|\*\=|\\\=|\=|\=\=|\=\=\=|\+|\-|\*|\\|\+\=|\>|\<)[^\>|\/]', Operator),
(r'(\(|\)|\,|\;|\=|\-|\+|\*|\/|\>|\<|\:)', Operator),
],
}
223 changes: 223 additions & 0 deletions tests/examplefiles/gsql/test.gsql
@@ -0,0 +1,223 @@
CREATE QUERY Member_Likeness(VERTEX <motionMember> m1, STRING inDate) FOR GRAPH motionData {
# TYPEDEF TUPLE <x FLOAT, y FLOAT> XYPair;
MapAccum<VERTEX<motionMember>, MapAccum<STRING, FLOAT>> @@likenessAccum;
MapAccum<VERTEX<motionMember>, FLOAT> @@BirthYearAccum;
MapAccum<VERTEX<motionMember>, FLOAT> @@HeightAccum;
MapAccum<VERTEX<motionMember>, FLOAT> @@WeightAccum;
ListAccum<VERTEX<location>> @@MemLocAccum;
MapAccum<VERTEX<motionMember>, FLOAT> @@LocAccum;
MapAccum<VERTEX<motionMember>, BagAccum<VERTEX<incentive>>> @@DayIncentives;
MapAccum<VERTEX<motionMember>, MapAccum<STRING, FLOAT>> @@MemberStats;
BagAccum<VERTEX<incentive>> @MemberIncentives;
AvgAccum @@StepsAccum;
AvgAccum @@BoutsAccum;
AvgAccum @@MilesAccum;

# Universal Vars
INT lastMax = 0;
INT lastMin = 1000;
FLOAT mult;

# Age Vars
INT ageRange;
INT birthYear;

# Height Vars
INT heightRange;
INT height;

# Height Vars
INT weightRange;
INT weight;

# Location Vars
FLOAT locRange;
VERTEX memLoc;

# Activity Vars
DATETIME lastRecording;

# lastRecording = to_datetime("2018-05-19");
lastRecording = to_datetime(inDate);

/*

test comment block

*/


members = {motionMember.*};

birthYear = Get_Birth_Year(m1);
height = m1.Height;
weight = m1.Weight;
temp = SELECT loc FROM members:member -(:e) - location: loc
WHERE member == m1
ACCUM
@@MemLocAccum += loc;
FOREACH loc in @@MemLocAccum DO
memLoc = loc;
END;

PRINT memLoc;
PRINT birthYear;
PRINT height;
PRINT weight;

results = SELECT member FROM members:member
ACCUM
@@BirthYearAccum += (member -> abs(Get_Birth_Year(member) - birthYear));

FOREACH (member,bys) in @@BirthYearAccum DO
IF bys > lastMax THEN
lastMax = bys;
END;
IF bys < lastMin THEN
lastMin = bys;
END;
END;

ageRange = lastMax - lastMin;
print ageRange;
mult = 1.0/ageRange;


FOREACH (member,bys) in @@BirthYearAccum DO
bys = 1 - bys * mult;
@@likenessAccum += (member -> ("age" -> bys));
END;

lastMax = 0;
lastMin = 1000;
mult = 0;

results = SELECT member FROM members:member
ACCUM
@@HeightAccum += (member -> abs(member.Height - height));

FOREACH (member,heights) in @@HeightAccum DO
IF heights < height THEN
IF heights > lastMax THEN
lastMax = heights;
END;
IF heights < lastMin THEN
lastMin = heights;
END;
END;
END;

heightRange = lastMax - lastMin;
print heightRange;
mult = 1.0/heightRange;


FOREACH (member,heights) in @@HeightAccum DO
IF heights < height THEN
heights = 1 - heights * mult;
ELSE
heights = 0;
END;
@@likenessAccum += (member -> ("height" -> heights));
END;

lastMax = 0;
lastMin = 1000;
mult = 0;

results = SELECT member FROM members:member
ACCUM
@@WeightAccum += (member -> abs(member.Weight - weight));

FOREACH (member,weights) in @@WeightAccum DO
IF weights < weight THEN
IF weights > lastMax THEN
lastMax = weights;
END;
IF weights < lastMin THEN
lastMin = weights;
END;
END;
END;

weightRange = lastMax - lastMin;
print weightRange;
mult = 1.0/weightRange;


FOREACH (member,weights) in @@WeightAccum DO
IF weights < weight THEN
weights = 1 - weights * mult;
ELSE
weights = 0;
END;
@@likenessAccum += (member -> ("weight" -> weights));
END;

lastMax = 0;
lastMin = 1000;
mult = 0;

resultsLoc = SELECT loc FROM members:member -(:e) - location: loc
ACCUM
@@LocAccum += (member -> Check_Distance(loc,memLoc));
FOREACH (member,loc) in @@LocAccum DO
IF loc < 5800 THEN
IF loc > lastMax THEN
lastMax = ceil(loc);
END;
IF loc < lastMin THEN
lastMin = floor(loc);
END;
END;
END;

PRINT lastMax;
PRINT lastMin;

locRange = lastMax - lastMin;
print locRange;
mult = 1.0/locRange;

FOREACH (member,loc) in @@LocAccum DO
IF loc > 5800 THEN
loc = -1;
ELSE
loc = 1 - loc * mult;
END;
@@likenessAccum += (member -> ("distance" -> loc));
END;

lastMax = 0;
lastMin = 1000;
mult = 0;

incentives = {incentive.*};

incentives = SELECT incent FROM incentives:incent - (:e) - lookupRule:rule WHERE
rule.RuleName == "Tenacity" AND incent.IncentiveDate > datetime_sub(lastRecording, INTERVAL 1 MONTH) AND incent.IncentiveDate < lastRecording;

members = SELECT member FROM incentives:incent - (:e) - motionMember:member
ACCUM
@@DayIncentives += (member -> (incent));

FOREACH (member, incent) IN @@DayIncentives DO
@@StepsAccum = 0;
FOREACH Incentive IN incent DO
@@StepsAccum += Incentive.TotalSteps;
@@BoutsAccum += Incentive.TotalBouts;
@@MilesAccum += Incentive.Miles;
END;
@@MemberStats += (member -> ("stepsAvg" -> @@StepsAccum));
@@MemberStats += (member -> ("stepsSlope" -> Linear_Regression(incent, 1)));
# @@MemberStats += (member -> ("boutsAvg" -> @@BoutsAccum));
# @@MemberStats += (member -> ("boutsSlope" -> Linear_Regression(incent, 2)));
@@MemberStats += (member -> ("milesAvg" -> @@MilesAccum));
@@MemberStats += (member -> ("milesSlope" -> Linear_Regression(incent, 3)));
END;


PRINT @@MemberStats;
PRINT @@likenessAccum;

}