1
0
mirror of https://github.com/alecthomas/chroma.git synced 2025-03-17 20:58:08 +02:00

Add QML lexer

This commit is contained in:
Carl Schwan 2020-09-24 19:51:43 +02:00 committed by Alec Thomas
parent 42e9638ed4
commit c88ec01ff7
3 changed files with 449 additions and 0 deletions

54
lexers/qml.go Normal file
View File

@ -0,0 +1,54 @@
package lexers
import (
. "github.com/alecthomas/chroma" // nolint
"github.com/alecthomas/chroma/lexers/internal"
)
// Qml lexer.
var Qml = internal.Register(MustNewLexer(
&Config{
Name: "QML",
Aliases: []string{"qml", "qbs"},
Filenames: []string{"*.qml", "*.qbs"},
MimeTypes: []string{"application/x-qml", "application/x-qt.qbs+qml"},
DotAll: true,
},
Rules{
"commentsandwhitespace": {
{`\s+`, Text, nil},
{`<!--`, Comment, nil},
{`//.*?\n`, CommentSingle, nil},
{`/\*.*?\*/`, CommentMultiline, nil},
},
"slashstartsregex": {
Include("commentsandwhitespace"),
{`/(\\.|[^[/\\\n]|\[(\\.|[^\]\\\n])*])+/([gim]+\b|\B)`, LiteralStringRegex, Pop(1)},
{`(?=/)`, Text, Push("#pop", "badregex")},
Default(Pop(1)),
},
"badregex": {
{`\n`, Text, Pop(1)},
},
"root": {
{`^(?=\s|/|<!--)`, Text, Push("slashstartsregex")},
Include("commentsandwhitespace"),
{`\+\+|--|~|&&|\?|:|\|\||\\(?=\n)|(<<|>>>?|==?|!=?|[-<>+*%&|^/])=?`, Operator, Push("slashstartsregex")},
{`[{(\[;,]`, Punctuation, Push("slashstartsregex")},
{`[})\].]`, Punctuation, nil},
{`\bid\s*:\s*[A-Za-z][\w.]*`, KeywordDeclaration, Push("slashstartsregex")},
{`\b[A-Za-z][\w.]*\s*:`, Keyword, Push("slashstartsregex")},
{`(for|in|while|do|break|return|continue|switch|case|default|if|else|throw|try|catch|finally|new|delete|typeof|instanceof|void|this)\b`, Keyword, Push("slashstartsregex")},
{`(var|let|with|function)\b`, KeywordDeclaration, Push("slashstartsregex")},
{`(abstract|boolean|byte|char|class|const|debugger|double|enum|export|extends|final|float|goto|implements|import|int|interface|long|native|package|private|protected|public|short|static|super|synchronized|throws|transient|volatile)\b`, KeywordReserved, nil},
{`(true|false|null|NaN|Infinity|undefined)\b`, KeywordConstant, nil},
{`(Array|Boolean|Date|Error|Function|Math|netscape|Number|Object|Packages|RegExp|String|sun|decodeURI|decodeURIComponent|encodeURI|encodeURIComponent|Error|eval|isFinite|isNaN|parseFloat|parseInt|document|this|window)\b`, NameBuiltin, nil},
{`[$a-zA-Z_]\w*`, NameOther, nil},
{`[0-9][0-9]*\.[0-9]+([eE][0-9]+)?[fd]?`, LiteralNumberFloat, nil},
{`0x[0-9a-fA-F]+`, LiteralNumberHex, nil},
{`[0-9]+`, LiteralNumberInteger, nil},
{`"(\\\\|\\"|[^"])*"`, LiteralStringDouble, nil},
{`'(\\\\|\\'|[^'])*'`, LiteralStringSingle, nil},
},
},
))

59
lexers/testdata/qml.actual vendored Normal file
View File

@ -0,0 +1,59 @@
import QtQuick 2.12
import QtQuick.Controls 2.15 as QQC2
import org.kde.kirigami 2.7 as Kirigami
import org.kde.kcm 1.2
import org.kde.kinfocenter.nic.private 1.0
ScrollViewKCM {
ConfigModule.quickHelp: i18n("Network Information")
clip: true
view: TableView {
id: tableview
clip: true
columnWidthProvider: function (column) {
return tableview.model ? tableview.width / tableview.model.columnCount() : 0
}
model: NetworkModel {
id: model
}
delegate: Rectangle {
readonly property real cellPadding: 8
readonly property color borderColor: Kirigami.Theme.Color
color: row % 2 === 0 ? "transparent" : Kirigami.Theme.alternateBackgroundColor
implicitWidth: tableview.width / tableview.model.columnCount()
implicitHeight: text.contentHeight + Kirigami.Units.largeSpacing
Text {
id: text
text: display
x: Kirigami.Units.smallSpacing
width: parent.width - Kirigami.Units.largeSpacing
height: parent.height
horizontalAlignment: Text.AlignHCenter
verticalAlignment: Text.AlignVCenter
color: "#ff26282a"
wrapMode: Text.WrapAnywhere
}
}
topMargin: headerView.implicitHeight
QQC2.HorizontalHeaderView {
id: headerView
anchors.bottom: parent.top
syncView: tableview
}
}
footer: QQC2.Button {
text: i18nc("Update the information displayed", "Update")
onClicked: model.update();
}
}

336
lexers/testdata/qml.expected vendored Normal file
View File

@ -0,0 +1,336 @@
[
{"type":"KeywordReserved","value":"import"},
{"type":"Text","value":" "},
{"type":"NameOther","value":"QtQuick"},
{"type":"Text","value":" "},
{"type":"LiteralNumberFloat","value":"2.12"},
{"type":"Text","value":"\n"},
{"type":"KeywordReserved","value":"import"},
{"type":"Text","value":" "},
{"type":"NameOther","value":"QtQuick"},
{"type":"Punctuation","value":"."},
{"type":"NameOther","value":"Controls"},
{"type":"Text","value":" "},
{"type":"LiteralNumberFloat","value":"2.15"},
{"type":"Text","value":" "},
{"type":"NameOther","value":"as"},
{"type":"Text","value":" "},
{"type":"NameOther","value":"QQC2"},
{"type":"Text","value":"\n\n"},
{"type":"KeywordReserved","value":"import"},
{"type":"Text","value":" "},
{"type":"NameOther","value":"org"},
{"type":"Punctuation","value":"."},
{"type":"NameOther","value":"kde"},
{"type":"Punctuation","value":"."},
{"type":"NameOther","value":"kirigami"},
{"type":"Text","value":" "},
{"type":"LiteralNumberFloat","value":"2.7"},
{"type":"Text","value":" "},
{"type":"NameOther","value":"as"},
{"type":"Text","value":" "},
{"type":"NameOther","value":"Kirigami"},
{"type":"Text","value":"\n"},
{"type":"KeywordReserved","value":"import"},
{"type":"Text","value":" "},
{"type":"NameOther","value":"org"},
{"type":"Punctuation","value":"."},
{"type":"NameOther","value":"kde"},
{"type":"Punctuation","value":"."},
{"type":"NameOther","value":"kcm"},
{"type":"Text","value":" "},
{"type":"LiteralNumberFloat","value":"1.2"},
{"type":"Text","value":"\n"},
{"type":"KeywordReserved","value":"import"},
{"type":"Text","value":" "},
{"type":"NameOther","value":"org"},
{"type":"Punctuation","value":"."},
{"type":"NameOther","value":"kde"},
{"type":"Punctuation","value":"."},
{"type":"NameOther","value":"kinfocenter"},
{"type":"Punctuation","value":"."},
{"type":"NameOther","value":"nic"},
{"type":"Punctuation","value":"."},
{"type":"KeywordReserved","value":"private"},
{"type":"Text","value":" "},
{"type":"LiteralNumberFloat","value":"1.0"},
{"type":"Text","value":"\n\n"},
{"type":"NameOther","value":"ScrollViewKCM"},
{"type":"Text","value":" "},
{"type":"Punctuation","value":"{"},
{"type":"Text","value":"\n "},
{"type":"Keyword","value":"ConfigModule.quickHelp:"},
{"type":"Text","value":" "},
{"type":"NameOther","value":"i18n"},
{"type":"Punctuation","value":"("},
{"type":"LiteralStringDouble","value":"\"Network Information\""},
{"type":"Punctuation","value":")"},
{"type":"Text","value":"\n "},
{"type":"Keyword","value":"clip:"},
{"type":"Text","value":" "},
{"type":"KeywordConstant","value":"true"},
{"type":"Text","value":"\n \n "},
{"type":"Keyword","value":"view:"},
{"type":"Text","value":" "},
{"type":"NameOther","value":"TableView"},
{"type":"Text","value":" "},
{"type":"Punctuation","value":"{"},
{"type":"Text","value":"\n "},
{"type":"KeywordDeclaration","value":"id: tableview"},
{"type":"Text","value":"\n "},
{"type":"Keyword","value":"clip:"},
{"type":"Text","value":" "},
{"type":"KeywordConstant","value":"true"},
{"type":"Text","value":"\n \n "},
{"type":"Keyword","value":"columnWidthProvider:"},
{"type":"Text","value":" "},
{"type":"KeywordDeclaration","value":"function"},
{"type":"Text","value":" "},
{"type":"Punctuation","value":"("},
{"type":"NameOther","value":"column"},
{"type":"Punctuation","value":")"},
{"type":"Text","value":" "},
{"type":"Punctuation","value":"{"},
{"type":"Text","value":" \n "},
{"type":"Keyword","value":"return"},
{"type":"Text","value":" "},
{"type":"NameOther","value":"tableview"},
{"type":"Punctuation","value":"."},
{"type":"NameOther","value":"model"},
{"type":"Text","value":" "},
{"type":"Operator","value":"?"},
{"type":"Text","value":" "},
{"type":"NameOther","value":"tableview"},
{"type":"Punctuation","value":"."},
{"type":"NameOther","value":"width"},
{"type":"Text","value":" "},
{"type":"Operator","value":"/"},
{"type":"Text","value":" "},
{"type":"NameOther","value":"tableview"},
{"type":"Punctuation","value":"."},
{"type":"NameOther","value":"model"},
{"type":"Punctuation","value":"."},
{"type":"NameOther","value":"columnCount"},
{"type":"Punctuation","value":"()"},
{"type":"Text","value":" "},
{"type":"Operator","value":":"},
{"type":"Text","value":" "},
{"type":"LiteralNumberInteger","value":"0"},
{"type":"Text","value":"\n "},
{"type":"Punctuation","value":"}"},
{"type":"Text","value":"\n\n "},
{"type":"Keyword","value":"model:"},
{"type":"Text","value":" "},
{"type":"NameOther","value":"NetworkModel"},
{"type":"Text","value":" "},
{"type":"Punctuation","value":"{"},
{"type":"Text","value":"\n "},
{"type":"KeywordDeclaration","value":"id: model"},
{"type":"Text","value":"\n "},
{"type":"Punctuation","value":"}"},
{"type":"Text","value":"\n\n "},
{"type":"Keyword","value":"delegate:"},
{"type":"Text","value":" "},
{"type":"NameOther","value":"Rectangle"},
{"type":"Text","value":" "},
{"type":"Punctuation","value":"{"},
{"type":"Text","value":"\n "},
{"type":"NameOther","value":"readonly"},
{"type":"Text","value":" "},
{"type":"NameOther","value":"property"},
{"type":"Text","value":" "},
{"type":"NameOther","value":"real"},
{"type":"Text","value":" "},
{"type":"Keyword","value":"cellPadding:"},
{"type":"Text","value":" "},
{"type":"LiteralNumberInteger","value":"8"},
{"type":"Text","value":"\n "},
{"type":"NameOther","value":"readonly"},
{"type":"Text","value":" "},
{"type":"NameOther","value":"property"},
{"type":"Text","value":" "},
{"type":"NameOther","value":"color"},
{"type":"Text","value":" "},
{"type":"Keyword","value":"borderColor:"},
{"type":"Text","value":" "},
{"type":"NameOther","value":"Kirigami"},
{"type":"Punctuation","value":"."},
{"type":"NameOther","value":"Theme"},
{"type":"Punctuation","value":"."},
{"type":"NameOther","value":"Color"},
{"type":"Text","value":"\n "},
{"type":"Keyword","value":"color:"},
{"type":"Text","value":" "},
{"type":"NameOther","value":"row"},
{"type":"Text","value":" "},
{"type":"Operator","value":"%"},
{"type":"Text","value":" "},
{"type":"LiteralNumberInteger","value":"2"},
{"type":"Text","value":" "},
{"type":"Operator","value":"==="},
{"type":"Text","value":" "},
{"type":"LiteralNumberInteger","value":"0"},
{"type":"Text","value":" "},
{"type":"Operator","value":"?"},
{"type":"Text","value":" "},
{"type":"LiteralStringDouble","value":"\"transparent\""},
{"type":"Text","value":" "},
{"type":"Operator","value":":"},
{"type":"Text","value":" "},
{"type":"NameOther","value":"Kirigami"},
{"type":"Punctuation","value":"."},
{"type":"NameOther","value":"Theme"},
{"type":"Punctuation","value":"."},
{"type":"NameOther","value":"alternateBackgroundColor"},
{"type":"Text","value":"\n \n "},
{"type":"Keyword","value":"implicitWidth:"},
{"type":"Text","value":" "},
{"type":"NameOther","value":"tableview"},
{"type":"Punctuation","value":"."},
{"type":"NameOther","value":"width"},
{"type":"Text","value":" "},
{"type":"Operator","value":"/"},
{"type":"Text","value":" "},
{"type":"NameOther","value":"tableview"},
{"type":"Punctuation","value":"."},
{"type":"NameOther","value":"model"},
{"type":"Punctuation","value":"."},
{"type":"NameOther","value":"columnCount"},
{"type":"Punctuation","value":"()"},
{"type":"Text","value":"\n "},
{"type":"Keyword","value":"implicitHeight:"},
{"type":"Text","value":" "},
{"type":"NameOther","value":"text"},
{"type":"Punctuation","value":"."},
{"type":"NameOther","value":"contentHeight"},
{"type":"Text","value":" "},
{"type":"Operator","value":"+"},
{"type":"Text","value":" "},
{"type":"NameOther","value":"Kirigami"},
{"type":"Punctuation","value":"."},
{"type":"NameOther","value":"Units"},
{"type":"Punctuation","value":"."},
{"type":"NameOther","value":"largeSpacing"},
{"type":"Text","value":"\n \n "},
{"type":"NameOther","value":"Text"},
{"type":"Text","value":" "},
{"type":"Punctuation","value":"{"},
{"type":"Text","value":"\n "},
{"type":"KeywordDeclaration","value":"id: text"},
{"type":"Text","value":"\n "},
{"type":"Keyword","value":"text:"},
{"type":"Text","value":" "},
{"type":"NameOther","value":"display"},
{"type":"Text","value":"\n "},
{"type":"Keyword","value":"x:"},
{"type":"Text","value":" "},
{"type":"NameOther","value":"Kirigami"},
{"type":"Punctuation","value":"."},
{"type":"NameOther","value":"Units"},
{"type":"Punctuation","value":"."},
{"type":"NameOther","value":"smallSpacing"},
{"type":"Text","value":"\n "},
{"type":"Keyword","value":"width:"},
{"type":"Text","value":" "},
{"type":"NameOther","value":"parent"},
{"type":"Punctuation","value":"."},
{"type":"NameOther","value":"width"},
{"type":"Text","value":" "},
{"type":"Operator","value":"-"},
{"type":"Text","value":" "},
{"type":"NameOther","value":"Kirigami"},
{"type":"Punctuation","value":"."},
{"type":"NameOther","value":"Units"},
{"type":"Punctuation","value":"."},
{"type":"NameOther","value":"largeSpacing"},
{"type":"Text","value":"\n "},
{"type":"Keyword","value":"height:"},
{"type":"Text","value":" "},
{"type":"NameOther","value":"parent"},
{"type":"Punctuation","value":"."},
{"type":"NameOther","value":"height"},
{"type":"Text","value":"\n "},
{"type":"Keyword","value":"horizontalAlignment:"},
{"type":"Text","value":" "},
{"type":"NameOther","value":"Text"},
{"type":"Punctuation","value":"."},
{"type":"NameOther","value":"AlignHCenter"},
{"type":"Text","value":"\n "},
{"type":"Keyword","value":"verticalAlignment:"},
{"type":"Text","value":" "},
{"type":"NameOther","value":"Text"},
{"type":"Punctuation","value":"."},
{"type":"NameOther","value":"AlignVCenter"},
{"type":"Text","value":"\n "},
{"type":"Keyword","value":"color:"},
{"type":"Text","value":" "},
{"type":"LiteralStringDouble","value":"\"#ff26282a\""},
{"type":"Text","value":"\n "},
{"type":"Keyword","value":"wrapMode:"},
{"type":"Text","value":" "},
{"type":"NameOther","value":"Text"},
{"type":"Punctuation","value":"."},
{"type":"NameOther","value":"WrapAnywhere"},
{"type":"Text","value":"\n "},
{"type":"Punctuation","value":"}"},
{"type":"Text","value":"\n "},
{"type":"Punctuation","value":"}"},
{"type":"Text","value":"\n \n "},
{"type":"Keyword","value":"topMargin:"},
{"type":"Text","value":" "},
{"type":"NameOther","value":"headerView"},
{"type":"Punctuation","value":"."},
{"type":"NameOther","value":"implicitHeight"},
{"type":"Text","value":"\n \n "},
{"type":"NameOther","value":"QQC2"},
{"type":"Punctuation","value":"."},
{"type":"NameOther","value":"HorizontalHeaderView"},
{"type":"Text","value":" "},
{"type":"Punctuation","value":"{"},
{"type":"Text","value":"\n "},
{"type":"KeywordDeclaration","value":"id: headerView"},
{"type":"Text","value":"\n "},
{"type":"Keyword","value":"anchors.bottom:"},
{"type":"Text","value":" "},
{"type":"NameOther","value":"parent"},
{"type":"Punctuation","value":"."},
{"type":"NameOther","value":"top"},
{"type":"Text","value":"\n "},
{"type":"Keyword","value":"syncView:"},
{"type":"Text","value":" "},
{"type":"NameOther","value":"tableview"},
{"type":"Text","value":"\n "},
{"type":"Punctuation","value":"}"},
{"type":"Text","value":"\n "},
{"type":"Punctuation","value":"}"},
{"type":"Text","value":"\n \n "},
{"type":"Keyword","value":"footer:"},
{"type":"Text","value":" "},
{"type":"NameOther","value":"QQC2"},
{"type":"Punctuation","value":"."},
{"type":"NameOther","value":"Button"},
{"type":"Text","value":" "},
{"type":"Punctuation","value":"{"},
{"type":"Text","value":"\n "},
{"type":"Keyword","value":"text:"},
{"type":"Text","value":" "},
{"type":"NameOther","value":"i18nc"},
{"type":"Punctuation","value":"("},
{"type":"LiteralStringDouble","value":"\"Update the information displayed\""},
{"type":"Punctuation","value":","},
{"type":"Text","value":" "},
{"type":"LiteralStringDouble","value":"\"Update\""},
{"type":"Punctuation","value":")"},
{"type":"Text","value":"\n "},
{"type":"Keyword","value":"onClicked:"},
{"type":"Text","value":" "},
{"type":"NameOther","value":"model"},
{"type":"Punctuation","value":"."},
{"type":"NameOther","value":"update"},
{"type":"Punctuation","value":"();"},
{"type":"Text","value":"\n "},
{"type":"Punctuation","value":"}"},
{"type":"Text","value":"\n"},
{"type":"Punctuation","value":"}"},
{"type":"Text","value":"\n\n"}
]