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

Adding Smithy Lexer #1930

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
16 changes: 16 additions & 0 deletions lib/rouge/demos/smithy
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
$version: "2"
namespace example.weather

service Weather {
version: "2006-03-01"
resources: [City]
operations: [GetCurrentTime]
}

resource City {
identifiers: { cityId: CityId }
read: GetCity
list: ListCities
resources: [Forecast]
}

114 changes: 114 additions & 0 deletions lib/rouge/lexers/smithy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
# -*- coding: utf-8 -*- #
# frozen_string_literal: true

module Rouge
module Lexers
class Smithy < RegexLexer
tag 'smithy'
filenames '*.smithy'

title 'Smithy'
desc 'Smithy is a language for defining services and SDKs (https://smithy.io).'

state :whitespace do
rule %r/\s+/, Text::Whitespace
end

state :key do
rule %r/\w+\:/, Name::Attribute
end

state :comment do
rule %r/\/\/.*/, Comment::Single
end

state :annotation do
rule %r/@\w+([.#]\w+)*/, Name::Decorator
end

state :simple_string do
rule %r/"/, Str::Double, :string
end

state :root do
mixin :whitespace
mixin :comment
mixin :annotation

rule %r/^\$version:/, Keyword::Reserved
rule %r/^\w+/, Keyword::Reserved
mixin :simple_string

rule %r/\(/, Punctuation, :arguments
rule %r/\[/, Punctuation, :array
rule %r/\{/, Punctuation, :object

rule %r/\w+([.#]\w+)*/, Name::Class
end

state :arguments do
rule %r/"\w+(\.\w+)*":/, Name::Attribute
mixin :whitespace
mixin :simple_string

rule %r/,/, Punctuation

rule %r/(true|false)/, Keyword::Constant
rule %r/\d+(\.\d+)?/, Num
rule %r/\[/, Punctuation, :array
rule %r/\{/, Punctuation, :object
rule %r/\w+:/, Name::Attribute
rule %r/\)/, Punctuation, :pop!
end

state :value do
mixin :whitespace
mixin :comment
mixin :annotation

rule %r/:/, Punctuation

rule %r/\$\w+/, Name::Label
mixin :simple_string
rule %r/\[/, Punctuation, :array
rule %r/\{/, Punctuation, :object
rule %r/\w+:/, Name::Attribute
rule %r/(\w+)(\s)(:=)/ do
groups Name::Attribute, Text::Whitespace, Punctuation
end
rule %r/[\w.#]+/, Name::Entity

end

state :string do
rule %r/[^"]/, Str::Double
rule %r/"/, Str::Double, :pop!
end


state :array do
mixin :whitespace
mixin :value
mixin :key


rule %r/,/, Punctuation
rule %r/\]/, Punctuation, :pop!
end

state :object do
mixin :annotation
rule %r/\(/, Punctuation, :arguments
rule %r/:=/, Punctuation
rule %r/\=/, Punctuation
mixin :whitespace
mixin :value
mixin :key


rule %r/\}/, Punctuation, :pop!
rule %r/,/, Punctuation
end
end
end
end
24 changes: 24 additions & 0 deletions spec/lexers/smithy_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# -*- coding: utf-8 -*- #
# frozen_string_literal: true

describe Rouge::Lexers::Smithy do
let(:subject) { Rouge::Lexers::Smithy.new }

describe 'guessing' do
include Support::Guessing

it 'guesses by filename' do
assert_guess :filename => 'foo.smithy'
end

end

describe 'lexing' do
include Support::Lexing

it 'recognizes annotation with dots and hashes' do
assert_tokens_equal '@aws.apigateway#integration', ['Name.Decorator', '@aws.apigateway#integration']
end
end
end

15 changes: 15 additions & 0 deletions spec/visual/samples/smithy
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
$version: "2"
namespace example.weather

service Weather {
version: "2006-03-01"
resources: [City]
operations: [GetCurrentTime]
}

resource City {
identifiers: { cityId: CityId }
read: GetCity
list: ListCities
resources: [Forecast]
}