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

Named Arguments #625

Merged
merged 32 commits into from
Sep 17, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
8ac5b1d
Initial commit for semantic tokenizer.
gdotdesign Jun 12, 2023
60f1bb5
Simplify semantic tokenizer a bit.
gdotdesign Jun 13, 2023
0e865c6
Working language server implementation.
gdotdesign Jun 13, 2023
1c14a5a
Cleanup semantic tokenizer class.
gdotdesign Jun 13, 2023
ca01ad6
Save keywords automatically instead of manually.
gdotdesign Jun 13, 2023
6926d57
Use an array derived from the actual token types.
gdotdesign Jun 13, 2023
29fb79b
Implement suggestions from code review.
gdotdesign Jun 14, 2023
6b25fc8
Update src/ls/semantic_tokens.cr
gdotdesign Jun 14, 2023
0eea575
Implement HTML highlighting.
gdotdesign Jun 14, 2023
1cd96da
Implement highlight directive.
gdotdesign Jun 14, 2023
529117d
Avoid unnecessary interations.
gdotdesign Jun 14, 2023
79c3f11
Implement suggestions from code review.
gdotdesign Jun 14, 2023
6faec26
Use the ast from the workspace semantic tokens.
gdotdesign Jun 25, 2023
37db9b6
Implementation of localization language structures.
gdotdesign Jun 26, 2023
6e51ae0
Update operation.cr
gdotdesign Jul 18, 2023
673b361
Merge branch 'master' into locales
gdotdesign Jul 18, 2023
92833ca
Update test.
gdotdesign Jul 21, 2023
5e9eef7
Merge branch 'master' into locales
gdotdesign Aug 8, 2023
44ab595
Revert change to the operation formatting.
gdotdesign Aug 8, 2023
a8ecd78
Update Locale.md
gdotdesign Aug 8, 2023
cb99c78
Implement labelled calls.
gdotdesign Jul 21, 2023
dd254f8
Don't reorder arguments in the formatter.
gdotdesign Jul 22, 2023
630c080
Minor fixes.
gdotdesign Jul 26, 2023
d28c7a3
Merge branch 'master' into locales
gdotdesign Sep 6, 2023
095ab66
Merge branch 'locales' into labelled-calls
gdotdesign Sep 6, 2023
d171155
Merge branch 'master' into locales
gdotdesign Sep 7, 2023
88bd4cc
Merge branch 'locales' into labelled-calls
gdotdesign Sep 7, 2023
36c9d98
Apply suggestions from code review
gdotdesign Sep 7, 2023
a2a0c04
Update src/compilers/call.cr
gdotdesign Sep 7, 2023
687550d
Finish renaming an error from code review.
gdotdesign Sep 7, 2023
99a1bac
Merge branch 'master' into labelled-calls
gdotdesign Sep 17, 2023
99acd9f
Merge branch 'master' into labelled-calls
gdotdesign Sep 17, 2023
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
Next Next commit
Initial commit for semantic tokenizer.
  • Loading branch information
gdotdesign committed Jun 12, 2023
commit 8ac5b1d8037ab0883a34855e938adee93d7a1386
2 changes: 2 additions & 0 deletions src/all.cr
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ require "./documentation_generator/**"
require "./documentation_generator"
require "./documentation_server"

require "./semantic_tokenizer"

require "./test_runner/**"
require "./test_runner"

Expand Down
5 changes: 3 additions & 2 deletions src/ast.cr
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@ module Mint
Js

getter components, modules, records, stores, routes, providers
getter suites, enums, comments, nodes, unified_modules
getter suites, enums, comments, nodes, unified_modules, keywords

def initialize(@records = [] of RecordDefinition,
def initialize(@keywords = [] of Tuple(Int32, Int32),
@records = [] of RecordDefinition,
@unified_modules = [] of Module,
@components = [] of Component,
@providers = [] of Provider,
Expand Down
5 changes: 3 additions & 2 deletions src/ast/html_component.cr
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
module Mint
class Ast
class HtmlComponent < Node
getter attributes, children, component, comments, ref
getter attributes, children, component, comments, ref, closing_tag_position

def initialize(@attributes : Array(HtmlAttribute),
@closing_tag_position : Int32?,
@comments : Array(Comment),
@children : Array(Node),
@ref : Variable?,
@component : TypeId,
@ref : Variable?,
@input : Data,
@from : Int32,
@to : Int32)
Expand Down
2 changes: 2 additions & 0 deletions src/ast/html_element.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ module Mint
class Ast
class HtmlElement < Node
getter attributes, children, styles, tag, comments, ref
getter closing_tag_position

def initialize(@attributes : Array(HtmlAttribute),
@closing_tag_position : Int32?,
@comments : Array(Comment),
@styles : Array(HtmlStyle),
@children : Array(Node),
Expand Down
12 changes: 8 additions & 4 deletions src/ast/node.cr
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ module Mint
source.strip.includes?('\n')
end

protected def compute_position(lines, needle) : Position
def self.compute_position(lines, needle) : Position
line_start_pos, line = begin
left, right = 0, lines.size - 1
index = pos = 0
Expand Down Expand Up @@ -107,19 +107,23 @@ module Mint
{line, column}
end

getter location : Location do
def self.compute_location(input : Data, from, to)
# TODO: avoid creating this array for every (initial) call to `Node#location`
lines = [0]
@input.input.each_char_with_index do |ch, i|
input.input.each_char_with_index do |ch, i|
lines << i + 1 if ch == '\n'
end

Location.new(
filename: @input.file,
filename: input.file,
start: compute_position(lines, from),
end: compute_position(lines, to),
)
end

getter location : Location do
Node.compute_location(input, from, to)
end
end
end
end
1 change: 1 addition & 0 deletions src/cli.cr
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ module Mint
define_help description: "Mint"

register_sub_command "sandbox-server", type: SandboxServer
register_sub_command highlight, type: Highlight
register_sub_command install, type: Install
register_sub_command compile, type: Compile
register_sub_command version, type: Version
Expand Down
84 changes: 84 additions & 0 deletions src/commands/highlight.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
module Mint
class Cli < Admiral::Command
class Highlight < Admiral::Command
include Command

define_help description: "Returns the syntax highlighted version of the given file as HTML"

define_argument path,
description: "The path to the file"

def run
if path = arguments.path
ast =
Parser.parse(path)

tokenizer = SemanticTokenizer.new
tokenizer.tokenize(ast)

parts = [] of String | Tuple(String, SemanticTokenizer::TokenType)
contents = File.read(path)
position = 0

tokenizer.tokens.sort_by(&.from).each do |token|
if token.from > position
parts << contents[position, token.from - position]
end

parts << {contents[token.from, token.to - token.from], token.type}
position = token.to
end

if position < contents.size
parts << contents[position, contents.size]
end

result = parts.reduce("") do |memo, item|
memo + case item
in String
item
in Tuple(String, SemanticTokenizer::TokenType)
case item[1]
in SemanticTokenizer::TokenType::Type
item[0].colorize(:yellow)
in SemanticTokenizer::TokenType::TypeParameter
item[0].colorize(:light_yellow)
in SemanticTokenizer::TokenType::Variable
item[0].colorize(:dark_gray)
in SemanticTokenizer::TokenType::Class
item[0].colorize(:blue)
in SemanticTokenizer::TokenType::Struct
item[0].colorize.fore(:white).back(:red)
in SemanticTokenizer::TokenType::Namespace
item[0].colorize(:light_blue)
in SemanticTokenizer::TokenType::Function
item[0].colorize.fore(:white).back(:red)
in SemanticTokenizer::TokenType::Keyword
item[0].colorize(:magenta)
in SemanticTokenizer::TokenType::Property
item[0].colorize(:dark_gray).mode(:underline)
in SemanticTokenizer::TokenType::Comment
item[0].colorize(:light_gray)
in SemanticTokenizer::TokenType::Enum
item[0].colorize.fore(:white).back(:red)
in SemanticTokenizer::TokenType::EnumMember
item[0].colorize.fore(:white).back(:red)
in SemanticTokenizer::TokenType::String
item[0].colorize(:green)
in SemanticTokenizer::TokenType::Number
item[0].colorize.fore(:white).back(:red)
in SemanticTokenizer::TokenType::Regexp
item[0].colorize.fore(:white).back(:red)
in SemanticTokenizer::TokenType::Operator
item[0].colorize.fore(:white).back(:red)
end.to_s
# %(<span class="#{html_class}">#{item[0]}</span>)
end
end

print result
end
end
end
end
end
10 changes: 7 additions & 3 deletions src/parser.cr
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,8 @@ module Mint
# Consuming keywords
# ----------------------------------------------------------------------------

def keyword!(word, error) : Bool
keyword(word) || raise error
def keyword!(word, error, save : Bool = false) : Bool
keyword(word, save) || raise error
end

def keyword_ahead?(word) : Bool
Expand All @@ -178,8 +178,12 @@ module Mint
true
end

def keyword(word) : Bool
def keyword(word, save : Bool = false) : Bool
if keyword_ahead?(word)
if save
@ast.keywords << {position, position + word.size}
end

@position += word.size
true
else
Expand Down
2 changes: 1 addition & 1 deletion src/parsers/array_literal.cr
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ module Mint

type = start do
whitespace
next unless keyword "of"
next unless keyword("of", true)
whitespace
type_or_type_variable! ArrayLiteralExpectedTypeOrVariable
end
Expand Down
4 changes: 2 additions & 2 deletions src/parsers/case.cr
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ module Mint

def case_expression(for_css : Bool = false) : Ast::Case?
start do |start_position|
next unless keyword "case"
next unless keyword("case", true)

whitespace

parens = char! '('

whitespace
await = keyword "await"
await = keyword("await", true)

whitespace
condition = expression! CaseExpectedCondition
Expand Down
4 changes: 2 additions & 2 deletions src/parsers/component.cr
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ module Mint
start do |start_position|
comment = self.comment

global = keyword "global"
global = keyword("global", true)
whitespace

next unless keyword "component"
next unless keyword("component", true)
whitespace

name = type_id! ComponentExpectedName
Expand Down
4 changes: 2 additions & 2 deletions src/parsers/connect.cr
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ module Mint

def connect : Ast::Connect?
start do |start_position|
next unless keyword "connect"
next unless keyword("connect", true)
whitespace

store = type_id! ConnectExpectedType
whitespace

keyword! "exposing", ConnectExpectedExposing
keyword!("exposing", ConnectExpectedExposing, true)

keys = block(
opening_bracket: ConnectExpectedOpeningBracket,
Expand Down
2 changes: 1 addition & 1 deletion src/parsers/connect_variable.cr
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module Mint

whitespace

if keyword "as"
if keyword("as", true)
whitespace
name = variable! ConnectVariableExpectedAs
end
Expand Down
2 changes: 1 addition & 1 deletion src/parsers/constant.cr
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module Mint
comment = self.comment
whitespace

next unless keyword "const"
next unless keyword("const", true)
whitespace

name = variable_constant!
Expand Down
2 changes: 1 addition & 1 deletion src/parsers/css_font_face.cr
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module Mint

def css_font_face : Ast::CssFontFace?
start do |start_position|
next unless keyword "@font-face"
next unless keyword("@font-face", true)

definitions = block(
opening_bracket: CssFontFaceExpectedOpeningBracket,
Expand Down
2 changes: 1 addition & 1 deletion src/parsers/css_keyframes.cr
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module Mint

def css_keyframes : Ast::CssKeyframes?
start do |start_position|
next unless keyword "@keyframes"
next unless keyword("@keyframes", true)

whitespace

Expand Down
6 changes: 3 additions & 3 deletions src/parsers/decode.cr
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ module Mint

def decode : Ast::Decode?
start do |start_position|
next unless keyword "decode"
next unless keyword("decode", true)
next unless whitespace?
whitespace

unless keyword "as"
unless keyword("as", true)
expression = expression! DecodeExpectedExpression

whitespace
keyword! "as", DecodeExpectedAs
keyword!("as", DecodeExpectedAs, true)
end

whitespace
Expand Down
2 changes: 1 addition & 1 deletion src/parsers/directives/asset.cr
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module Mint

def asset_directive : Ast::Directives::Asset?
start do |start_position|
next unless keyword "@asset"
next unless keyword("@asset", true)

char '(', AssetDirectiveExpectedOpeningParentheses
whitespace
Expand Down
2 changes: 1 addition & 1 deletion src/parsers/directives/documentation.cr
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module Mint

def documentation_directive : Ast::Directives::Documentation?
start do |start_position|
next unless keyword "@documentation"
next unless keyword("@documentation", true)

char '(', DocumentationDirectiveExpectedOpeningParentheses
whitespace
Expand Down
2 changes: 1 addition & 1 deletion src/parsers/directives/format.cr
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module Mint

def format_directive : Ast::Directives::Format?
start do |start_position|
next unless keyword "@format"
next unless keyword("@format", true)

content =
code_block(
Expand Down
2 changes: 1 addition & 1 deletion src/parsers/directives/inline.cr
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module Mint

def inline_directive : Ast::Directives::Inline?
start do |start_position|
next unless keyword "@inline"
next unless keyword("@inline", true)

char '(', InlineDirectiveExpectedOpeningParentheses
whitespace
Expand Down
2 changes: 1 addition & 1 deletion src/parsers/directives/svg.cr
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module Mint

def svg_directive : Ast::Directives::Svg?
start do |start_position|
next unless keyword "@svg"
next unless keyword("@svg", true)

char '(', SvgDirectiveExpectedOpeningParentheses
whitespace
Expand Down
2 changes: 1 addition & 1 deletion src/parsers/encode.cr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Mint

def encode : Ast::Encode?
start do |start_position|
next unless keyword "encode"
next unless keyword("encode", true)
next unless whitespace?
whitespace

Expand Down
2 changes: 1 addition & 1 deletion src/parsers/enum.cr
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module Mint
start do |start_position|
comment = self.comment

next unless keyword "enum"
next unless keyword("enum", true)
whitespace

name = type_id! EnumExpectedName
Expand Down
4 changes: 2 additions & 2 deletions src/parsers/for.cr
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module Mint

def for_expression : Ast::For?
start do |start_position|
next unless keyword "for"
next unless keyword("for", true)
next unless whitespace?
whitespace

Expand All @@ -22,7 +22,7 @@ module Mint
) { variable }

whitespace
keyword! "of", ForExpectedOf
keyword!("of", ForExpectedOf, true)
whitespace

subject = expression! ForExpectedSubject
Expand Down
Loading