Skip to content

Commit

Permalink
SQL: Added identifier token (#3141)
Browse files Browse the repository at this point in the history
  • Loading branch information
RunDevelopment committed Oct 14, 2021
1 parent a8bacc7 commit 4e00cdd
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 1 deletion.
8 changes: 8 additions & 0 deletions components/prism-sql.js
Expand Up @@ -15,6 +15,14 @@ Prism.languages.sql = {
greedy: true,
lookbehind: true
},
'identifier': {
pattern: /(^|[^@\\])`(?:\\[\s\S]|[^`\\]|``)*`/,
greedy: true,
lookbehind: true,
inside: {
'punctuation': /^`|`$/
}
},
'function': /\b(?:AVG|COUNT|FIRST|FORMAT|LAST|LCASE|LEN|MAX|MID|MIN|MOD|NOW|ROUND|SUM|UCASE)(?=\s*\()/i, // Should we highlight user defined functions too?
'keyword': /\b(?:ACTION|ADD|AFTER|ALGORITHM|ALL|ALTER|ANALYZE|ANY|APPLY|AS|ASC|AUTHORIZATION|AUTO_INCREMENT|BACKUP|BDB|BEGIN|BERKELEYDB|BIGINT|BINARY|BIT|BLOB|BOOL|BOOLEAN|BREAK|BROWSE|BTREE|BULK|BY|CALL|CASCADED?|CASE|CHAIN|CHAR(?:ACTER|SET)?|CHECK(?:POINT)?|CLOSE|CLUSTERED|COALESCE|COLLATE|COLUMNS?|COMMENT|COMMIT(?:TED)?|COMPUTE|CONNECT|CONSISTENT|CONSTRAINT|CONTAINS(?:TABLE)?|CONTINUE|CONVERT|CREATE|CROSS|CURRENT(?:_DATE|_TIME|_TIMESTAMP|_USER)?|CURSOR|CYCLE|DATA(?:BASES?)?|DATE(?:TIME)?|DAY|DBCC|DEALLOCATE|DEC|DECIMAL|DECLARE|DEFAULT|DEFINER|DELAYED|DELETE|DELIMITERS?|DENY|DESC|DESCRIBE|DETERMINISTIC|DISABLE|DISCARD|DISK|DISTINCT|DISTINCTROW|DISTRIBUTED|DO|DOUBLE|DROP|DUMMY|DUMP(?:FILE)?|DUPLICATE|ELSE(?:IF)?|ENABLE|ENCLOSED|END|ENGINE|ENUM|ERRLVL|ERRORS|ESCAPED?|EXCEPT|EXEC(?:UTE)?|EXISTS|EXIT|EXPLAIN|EXTENDED|FETCH|FIELDS|FILE|FILLFACTOR|FIRST|FIXED|FLOAT|FOLLOWING|FOR(?: EACH ROW)?|FORCE|FOREIGN|FREETEXT(?:TABLE)?|FROM|FULL|FUNCTION|GEOMETRY(?:COLLECTION)?|GLOBAL|GOTO|GRANT|GROUP|HANDLER|HASH|HAVING|HOLDLOCK|HOUR|IDENTITY(?:COL|_INSERT)?|IF|IGNORE|IMPORT|INDEX|INFILE|INNER|INNODB|INOUT|INSERT|INT|INTEGER|INTERSECT|INTERVAL|INTO|INVOKER|ISOLATION|ITERATE|JOIN|KEYS?|KILL|LANGUAGE|LAST|LEAVE|LEFT|LEVEL|LIMIT|LINENO|LINES|LINESTRING|LOAD|LOCAL|LOCK|LONG(?:BLOB|TEXT)|LOOP|MATCH(?:ED)?|MEDIUM(?:BLOB|INT|TEXT)|MERGE|MIDDLEINT|MINUTE|MODE|MODIFIES|MODIFY|MONTH|MULTI(?:LINESTRING|POINT|POLYGON)|NATIONAL|NATURAL|NCHAR|NEXT|NO|NONCLUSTERED|NULLIF|NUMERIC|OFF?|OFFSETS?|ON|OPEN(?:DATASOURCE|QUERY|ROWSET)?|OPTIMIZE|OPTION(?:ALLY)?|ORDER|OUT(?:ER|FILE)?|OVER|PARTIAL|PARTITION|PERCENT|PIVOT|PLAN|POINT|POLYGON|PRECEDING|PRECISION|PREPARE|PREV|PRIMARY|PRINT|PRIVILEGES|PROC(?:EDURE)?|PUBLIC|PURGE|QUICK|RAISERROR|READS?|REAL|RECONFIGURE|REFERENCES|RELEASE|RENAME|REPEAT(?:ABLE)?|REPLACE|REPLICATION|REQUIRE|RESIGNAL|RESTORE|RESTRICT|RETURN(?:ING|S)?|REVOKE|RIGHT|ROLLBACK|ROUTINE|ROW(?:COUNT|GUIDCOL|S)?|RTREE|RULE|SAVE(?:POINT)?|SCHEMA|SECOND|SELECT|SERIAL(?:IZABLE)?|SESSION(?:_USER)?|SET(?:USER)?|SHARE|SHOW|SHUTDOWN|SIMPLE|SMALLINT|SNAPSHOT|SOME|SONAME|SQL|START(?:ING)?|STATISTICS|STATUS|STRIPED|SYSTEM_USER|TABLES?|TABLESPACE|TEMP(?:ORARY|TABLE)?|TERMINATED|TEXT(?:SIZE)?|THEN|TIME(?:STAMP)?|TINY(?:BLOB|INT|TEXT)|TOP?|TRAN(?:SACTIONS?)?|TRIGGER|TRUNCATE|TSEQUAL|TYPES?|UNBOUNDED|UNCOMMITTED|UNDEFINED|UNION|UNIQUE|UNLOCK|UNPIVOT|UNSIGNED|UPDATE(?:TEXT)?|USAGE|USE|USER|USING|VALUES?|VAR(?:BINARY|CHAR|CHARACTER|YING)|VIEW|WAITFOR|WARNINGS|WHEN|WHERE|WHILE|WITH(?: ROLLUP|IN)?|WORK|WRITE(?:TEXT)?|YEAR)\b/i,
'boolean': /\b(?:FALSE|NULL|TRUE)\b/i,
Expand Down
2 changes: 1 addition & 1 deletion components/prism-sql.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 41 additions & 0 deletions tests/languages/sql/identifier_feature.test
@@ -0,0 +1,41 @@
`5Customers`
`tableName~`
` SELECT `
foo.`GROUP`
`a``b`

----------------------------------------------------

[
["identifier", [
["punctuation", "`"],
"5Customers",
["punctuation", "`"]
]],

["identifier", [
["punctuation", "`"],
"tableName~",
["punctuation", "`"]
]],

["identifier", [
["punctuation", "`"],
" SELECT ",
["punctuation", "`"]
]],

"\r\nfoo",
["punctuation", "."],
["identifier", [
["punctuation", "`"],
"GROUP",
["punctuation", "`"]
]],

["identifier", [
["punctuation", "`"],
"a``b",
["punctuation", "`"]
]]
]
72 changes: 72 additions & 0 deletions tests/languages/sql/issue3140.test
@@ -0,0 +1,72 @@
select
`t`.`col1`, `t`.`col2`, `t`.`col3`, `t`.`col4`
from
`test_table` as `t`

----------------------------------------------------

[
["keyword", "select"],

["identifier", [
["punctuation", "`"],
"t",
["punctuation", "`"]
]],
["punctuation", "."],
["identifier", [
["punctuation", "`"],
"col1",
["punctuation", "`"]
]],
["punctuation", ","],
["identifier", [
["punctuation", "`"],
"t",
["punctuation", "`"]
]],
["punctuation", "."],
["identifier", [
["punctuation", "`"],
"col2",
["punctuation", "`"]
]],
["punctuation", ","],
["identifier", [
["punctuation", "`"],
"t",
["punctuation", "`"]
]],
["punctuation", "."],
["identifier", [
["punctuation", "`"],
"col3",
["punctuation", "`"]
]],
["punctuation", ","],
["identifier", [
["punctuation", "`"],
"t",
["punctuation", "`"]
]],
["punctuation", "."],
["identifier", [
["punctuation", "`"],
"col4",
["punctuation", "`"]
]],

["keyword", "from"],

["identifier", [
["punctuation", "`"],
"test_table",
["punctuation", "`"]
]],
["keyword", "as"],
["identifier", [
["punctuation", "`"],
"t",
["punctuation", "`"]
]]
]

0 comments on commit 4e00cdd

Please sign in to comment.