Skip to content

Commit

Permalink
Add basic lexer for Svelte
Browse files Browse the repository at this point in the history
  • Loading branch information
iamvukasin authored and alecthomas committed Apr 27, 2021
1 parent e86123a commit cab6ebc
Show file tree
Hide file tree
Showing 3 changed files with 283 additions and 0 deletions.
39 changes: 39 additions & 0 deletions lexers/s/svelte.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package s;

import (
. "github.com/alecthomas/chroma" // nolint
"github.com/alecthomas/chroma/lexers/h"
"github.com/alecthomas/chroma/lexers/internal"
"github.com/alecthomas/chroma/lexers/t"
)

// Svelte lexer.
var Svelte = internal.Register(DelegatingLexer(h.HTML, MustNewLazyLexer(
&Config{
Name: "Svelte",
Aliases: []string{"svelte"},
Filenames: []string{"*.svelte"},
MimeTypes: []string{"application/x-svelte"},
DotAll: true,
},
svelteRules,
)))

func svelteRules() Rules {
return Rules {
"root": {
{`(<\s*script\s*lang\s*=\s*['"](?:ts|typescript)['"]\s*>)(.+?)(<\s*/\s*script\s*>)`, ByGroups(Other, Using(t.TypeScript), Other), nil},
{`\{`, Punctuation, Push("templates")},
{`[^{]`, Other, nil},
},
"templates": {
{`}`, Punctuation, Pop(1)},
{`@(debug|html)\b`, Keyword, nil},
{`(#|/)(await|each|if)\b`, Keyword, nil},
{`(:else)(\s+)(if)?\b`, ByGroups(Keyword, Text, Keyword), nil},
{`:(catch|then)\b`, Keyword, nil},
{`then\b`, Keyword, nil},
{`[^}]+`, Using(t.TypeScript), nil},
},
}
}
33 changes: 33 additions & 0 deletions lexers/testdata/svelte.actual
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<script lang="typescript">
// Nested is defined elsewhere
import Nested from './Nested.svelte';

let src = 'example.png';
let name = 'world';

let names = [
{ id: 'id0001', name: 'Name 1' },
{ id: 'id0002', name: 'Name 2' },
{ id: 'id0003', name: 'Name 3' }
];
</script>

<style>
h1 {
color: red;
font-family: Arial, Helvetica, sans-serif;
font-size: 2em;
}
</style>

<h1>Hello {name}!</h1>
<img {src} alt="Example image">

<!-- import external component -->
<Nested/>

<ul>
{#each names as { id, name }}
<li>{name} ({id})</li>
{/each}
</ul>
211 changes: 211 additions & 0 deletions lexers/testdata/svelte.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,211 @@
[
{"type":"Punctuation","value":"\u003c"},
{"type":"NameTag","value":"script"},
{"type":"Text","value":" "},
{"type":"NameAttribute","value":"lang"},
{"type":"Operator","value":"="},
{"type":"LiteralString","value":"\"typescript\""},
{"type":"Punctuation","value":"\u003e"},
{"type":"Text","value":"\n\t"},
{"type":"CommentSingle","value":"// Nested is defined elsewhere\n"},
{"type":"Text","value":"\t"},
{"type":"KeywordReserved","value":"import"},
{"type":"Text","value":" "},
{"type":"NameOther","value":"Nested"},
{"type":"Text","value":" "},
{"type":"KeywordReserved","value":"from"},
{"type":"Text","value":" "},
{"type":"LiteralStringSingle","value":"'./Nested.svelte'"},
{"type":"Punctuation","value":";"},
{"type":"Text","value":"\n\n\t"},
{"type":"KeywordDeclaration","value":"let"},
{"type":"Text","value":" "},
{"type":"NameOther","value":"src"},
{"type":"Text","value":" "},
{"type":"Operator","value":"="},
{"type":"Text","value":" "},
{"type":"LiteralStringSingle","value":"'example.png'"},
{"type":"Punctuation","value":";"},
{"type":"Text","value":"\n\t"},
{"type":"KeywordDeclaration","value":"let"},
{"type":"Text","value":" "},
{"type":"NameOther","value":"name"},
{"type":"Text","value":" "},
{"type":"Operator","value":"="},
{"type":"Text","value":" "},
{"type":"LiteralStringSingle","value":"'world'"},
{"type":"Punctuation","value":";"},
{"type":"Text","value":"\n\n\t"},
{"type":"KeywordDeclaration","value":"let"},
{"type":"Text","value":" "},
{"type":"NameOther","value":"names"},
{"type":"Text","value":" "},
{"type":"Operator","value":"="},
{"type":"Text","value":" "},
{"type":"Punctuation","value":"["},
{"type":"Text","value":"\n\t\t"},
{"type":"Punctuation","value":"{"},
{"type":"Text","value":" "},
{"type":"NameOther","value":"id"},
{"type":"Operator","value":":"},
{"type":"Text","value":" "},
{"type":"LiteralStringSingle","value":"'id0001'"},
{"type":"Punctuation","value":","},
{"type":"Text","value":" "},
{"type":"NameOther","value":"name"},
{"type":"Operator","value":":"},
{"type":"Text","value":" "},
{"type":"LiteralStringSingle","value":"'Name 1'"},
{"type":"Text","value":" "},
{"type":"Punctuation","value":"},"},
{"type":"Text","value":"\n\t\t"},
{"type":"Punctuation","value":"{"},
{"type":"Text","value":" "},
{"type":"NameOther","value":"id"},
{"type":"Operator","value":":"},
{"type":"Text","value":" "},
{"type":"LiteralStringSingle","value":"'id0002'"},
{"type":"Punctuation","value":","},
{"type":"Text","value":" "},
{"type":"NameOther","value":"name"},
{"type":"Operator","value":":"},
{"type":"Text","value":" "},
{"type":"LiteralStringSingle","value":"'Name 2'"},
{"type":"Text","value":" "},
{"type":"Punctuation","value":"},"},
{"type":"Text","value":"\n\t\t"},
{"type":"Punctuation","value":"{"},
{"type":"Text","value":" "},
{"type":"NameOther","value":"id"},
{"type":"Operator","value":":"},
{"type":"Text","value":" "},
{"type":"LiteralStringSingle","value":"'id0003'"},
{"type":"Punctuation","value":","},
{"type":"Text","value":" "},
{"type":"NameOther","value":"name"},
{"type":"Operator","value":":"},
{"type":"Text","value":" "},
{"type":"LiteralStringSingle","value":"'Name 3'"},
{"type":"Text","value":" "},
{"type":"Punctuation","value":"}"},
{"type":"Text","value":"\n\t"},
{"type":"Punctuation","value":"];"},
{"type":"Text","value":"\n"},
{"type":"Punctuation","value":"\u003c/"},
{"type":"NameTag","value":"script"},
{"type":"Punctuation","value":"\u003e"},
{"type":"Text","value":"\n\n"},
{"type":"Punctuation","value":"\u003c"},
{"type":"NameTag","value":"style"},
{"type":"Punctuation","value":"\u003e"},
{"type":"Text","value":"\n\t"},
{"type":"NameTag","value":"h1"},
{"type":"Text","value":" "},
{"type":"Punctuation","value":"{"},
{"type":"Text","value":"\n\t\t"},
{"type":"NameOther","value":"color"},
{"type":"Text","value":": "},
{"type":"KeywordType","value":"red"},
{"type":"Punctuation","value":";"},
{"type":"Text","value":"\n\t\t"},
{"type":"NameOther","value":"font"},
{"type":"Operator","value":"-"},
{"type":"NameOther","value":"family"},
{"type":"Text","value":": "},
{"type":"KeywordType","value":"Arial"},
{"type":"Punctuation","value":","},
{"type":"Text","value":" "},
{"type":"NameOther","value":"Helvetica"},
{"type":"Punctuation","value":","},
{"type":"Text","value":" "},
{"type":"NameOther","value":"sans"},
{"type":"Operator","value":"-"},
{"type":"NameOther","value":"serif"},
{"type":"Punctuation","value":";"},
{"type":"Text","value":"\n\t\t"},
{"type":"NameOther","value":"font"},
{"type":"Operator","value":"-"},
{"type":"NameOther","value":"size"},
{"type":"Text","value":": "},
{"type":"KeywordType","value":"2em"},
{"type":"Punctuation","value":";"},
{"type":"Text","value":"\n\t"},
{"type":"Punctuation","value":"}"},
{"type":"Text","value":"\n"},
{"type":"Punctuation","value":"\u003c/"},
{"type":"NameTag","value":"style"},
{"type":"Punctuation","value":"\u003e"},
{"type":"Text","value":"\n\n"},
{"type":"Punctuation","value":"\u003c"},
{"type":"NameTag","value":"h1"},
{"type":"Punctuation","value":"\u003e"},
{"type":"Text","value":"Hello "},
{"type":"Punctuation","value":"{"},
{"type":"NameOther","value":"name"},
{"type":"Punctuation","value":"}"},
{"type":"Text","value":"!"},
{"type":"Punctuation","value":"\u003c/"},
{"type":"NameTag","value":"h1"},
{"type":"Punctuation","value":"\u003e"},
{"type":"Text","value":"\n"},
{"type":"Punctuation","value":"\u003c"},
{"type":"NameTag","value":"img"},
{"type":"Text","value":" "},
{"type":"Punctuation","value":"{"},
{"type":"NameOther","value":"src"},
{"type":"Punctuation","value":"}"},
{"type":"Text","value":" "},
{"type":"NameAttribute","value":"alt"},
{"type":"Operator","value":"="},
{"type":"LiteralString","value":"\"Example image\""},
{"type":"Punctuation","value":"\u003e"},
{"type":"Text","value":"\n\n"},
{"type":"Comment","value":"\u003c!-- import external component --\u003e"},
{"type":"Text","value":"\n"},
{"type":"Punctuation","value":"\u003c"},
{"type":"NameTag","value":"Nested"},
{"type":"Punctuation","value":"/\u003e"},
{"type":"Text","value":"\n\n"},
{"type":"Punctuation","value":"\u003c"},
{"type":"NameTag","value":"ul"},
{"type":"Punctuation","value":"\u003e"},
{"type":"Text","value":"\n\t"},
{"type":"Punctuation","value":"{"},
{"type":"Keyword","value":"#each"},
{"type":"Text","value":" "},
{"type":"NameOther","value":"names"},
{"type":"Text","value":" "},
{"type":"KeywordReserved","value":"as"},
{"type":"Text","value":" "},
{"type":"Punctuation","value":"{"},
{"type":"Text","value":" "},
{"type":"NameOther","value":"id"},
{"type":"Punctuation","value":","},
{"type":"Text","value":" "},
{"type":"NameOther","value":"name"},
{"type":"Text","value":" "},
{"type":"Punctuation","value":"}"},
{"type":"Text","value":"}\n\t\t"},
{"type":"Punctuation","value":"\u003c"},
{"type":"NameTag","value":"li"},
{"type":"Punctuation","value":"\u003e{"},
{"type":"NameOther","value":"name"},
{"type":"Punctuation","value":"}"},
{"type":"Text","value":" ("},
{"type":"Punctuation","value":"{"},
{"type":"NameOther","value":"id"},
{"type":"Punctuation","value":"}"},
{"type":"Text","value":")"},
{"type":"Punctuation","value":"\u003c/"},
{"type":"NameTag","value":"li"},
{"type":"Punctuation","value":"\u003e"},
{"type":"Text","value":"\n\t"},
{"type":"Punctuation","value":"{"},
{"type":"Keyword","value":"/each"},
{"type":"Punctuation","value":"}"},
{"type":"Text","value":"\n"},
{"type":"Punctuation","value":"\u003c/"},
{"type":"NameTag","value":"ul"},
{"type":"Punctuation","value":"\u003e"},
{"type":"Text","value":"\n"}
]

0 comments on commit cab6ebc

Please sign in to comment.