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
Prev Previous commit
Next Next commit
Implement highlight directive.
  • Loading branch information
gdotdesign committed Jun 14, 2023
commit 1cd96da0d2bc29cb1fdca5fba3004372bd19e636
15 changes: 15 additions & 0 deletions src/ast/directives/highlight.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module Mint
class Ast
module Directives
class Highlight < Node
getter content

def initialize(@content : Block,
@input : Data,
@from : Int32,
@to : Int32)
end
end
end
end
end
36 changes: 36 additions & 0 deletions src/compilers/directives/highlight.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
module Mint
class Compiler
def _compile(node : Ast::Directives::Highlight) : String
content =
compile node.content

formatted =
Formatter.new.format(node.content, Formatter::BlockFormat::Naked)

parser = Parser.new(formatted, "source.mint")
parser.code_block_naked

contents, parts =
SemanticTokenizer.tokenize(parser.ast)

mapped =
parts.map do |item|
case item
in String
"`#{skip { escape_for_javascript(item) }}`"
in Tuple(String, SemanticTokenizer::TokenType)
"_h('span', { className: '#{item[1].to_s.underscore}' }, [`#{skip { escape_for_javascript(item[0]) }}`])"
end
end

"[#{content}, _h(React.Fragment, {}, [#{mapped.join(",\n")}])]"
end

def escape_for_javascript(value : String)
value
.gsub('\\', "\\\\")
.gsub('`', "\\`")
.gsub("${", "\\${")
end
end
end
7 changes: 7 additions & 0 deletions src/formatters/directives/highlight.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module Mint
class Formatter
def format(node : Ast::Directives::Highlight)
"@highlight #{format(node.content)}"
end
end
end
1 change: 1 addition & 0 deletions src/ls/semantic_tokens.cr
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ module Mint
# This is used later on to convert the line/column of each token
input =
ast.nodes.first.input

tokenizer = SemanticTokenizer.new
tokenizer.tokenize(ast)

Expand Down
1 change: 1 addition & 0 deletions src/parsers/basic_expression.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module Mint
# NOTE: The order of the parsing is important!
def basic_expression : Ast::Expression?
format_directive ||
highlight_directive ||
documentation_directive ||
svg_directive ||
asset_directive ||
Expand Down
13 changes: 13 additions & 0 deletions src/parsers/code_block.cr
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
module Mint
class Parser
def code_block_naked : Ast::Block?
start do |start_position|
statements =
many { comment || statement }

self << Ast::Block.new(
statements: statements,
from: start_position,
to: position,
input: data) if statements
end
end

def code_block : Ast::Block?
start do |start_position|
statements =
Expand Down
25 changes: 25 additions & 0 deletions src/parsers/directives/highlight.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module Mint
class Parser
syntax_error FormatDirectiveExpectedOpeningBracket
syntax_error FormatDirectiveExpectedClosingBracket
syntax_error FormatDirectiveExpectedExpression

def highlight_directive : Ast::Directives::Highlight?
start do |start_position|
next unless keyword "@highlight"

content =
code_block(
opening_bracket: FormatDirectiveExpectedOpeningBracket,
closing_bracket: FormatDirectiveExpectedClosingBracket,
statement_error: FormatDirectiveExpectedExpression)

self << Ast::Directives::Highlight.new(
from: start_position,
content: content,
to: position,
input: data)
end
end
end
end
15 changes: 11 additions & 4 deletions src/semantic_tokenizer.cr
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,7 @@ module Mint
# This is where the resulting tokens are stored.
getter tokens : Array(Token) = [] of Token

def self.highlight(path : String, html : Bool = false)
ast =
Parser.parse(path)

def self.tokenize(ast : Ast)
tokenizer = self.new
tokenizer.tokenize(ast)

Expand All @@ -67,6 +64,16 @@ module Mint
parts << contents[position, contents.size]
end

{contents, parts}
end

def self.highlight(path : String, html : Bool = false)
ast =
Parser.parse(path)

contents, parts =
tokenize(ast)

parts.reduce("") do |memo, item|
memo + case item
in String
Expand Down
7 changes: 7 additions & 0 deletions src/type_checkers/directives/highlight.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module Mint
class TypeChecker
def check(node : Ast::Directives::Highlight) : Checkable
Type.new("Tuple", [resolve(node.content), HTML] of Checkable)
end
end
end