-
-
Notifications
You must be signed in to change notification settings - Fork 148
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement @highlight-file directive.
- Loading branch information
1 parent
db82722
commit a33506e
Showing
11 changed files
with
184 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
component Main { | ||
fun render : Html { | ||
@highlight-file(../../fixtures/Test.mint) | ||
} | ||
} | ||
-------------------------------------------------------------------------------- | ||
class A extends _C { | ||
render() { | ||
return _h(React.Fragment, {}, [_h('span', { className: 'keyword' }, [`component`]), | ||
` `, | ||
_h('span', { className: 'type' }, [`Main`]), | ||
` { | ||
`, | ||
_h('span', { className: 'keyword' }, [`fun`]), | ||
` render : `, | ||
_h('span', { className: 'type' }, [`Html`]), | ||
` { | ||
<`, | ||
_h('span', { className: 'namespace' }, [`div`]), | ||
`></`, | ||
_h('span', { className: 'namespace' }, [`div`]), | ||
`> | ||
} | ||
} | ||
`]); | ||
} | ||
}; | ||
|
||
A.displayName = "Main"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
--------------------------highlight_file_directive_expected_opening_parenthesis | ||
component Main { | ||
fun render : String { | ||
@highlight-file | ||
-----------------------------------------highlight_file_directive_expected_path | ||
component Main { | ||
fun render : String { | ||
@highlight-file( | ||
--------------------------highlight_file_directive_expected_closing_parenthesis | ||
component Main { | ||
fun render : String { | ||
@highlight-file(path | ||
-----------------------------------------highlight_file_directive_expected_file | ||
component Main { | ||
fun render : Html { | ||
@highlight-file(File.mint) | ||
} | ||
} | ||
-----------------------------------------highlight_file_directive_expected_mint | ||
component Main { | ||
fun render : Html { | ||
@highlight-file(../../fixtures/icon-not-svg) | ||
} | ||
} | ||
------------------------------------------------------------------------------- | ||
component Main { | ||
fun render : Html { | ||
@highlight-file(../../../core/source/Array.mint) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
component Main { | ||
fun render : Html { | ||
<div></div> | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
component Main { | ||
fun render : Html { | ||
@highlight-file(../../fixtures/Test.mint) | ||
} | ||
} | ||
-------------------------------------------------------------------------------- | ||
component Main { | ||
fun render : Html { | ||
@highlight-file(../../fixtures/Test.mint) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,6 +49,9 @@ module Mint | |
|
||
class Svg < FileBased | ||
end | ||
|
||
class HighlightFile < FileBased | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
module Mint | ||
class Compiler | ||
def _compile(node : Ast::Directives::HighlightFile) : String | ||
contents = | ||
File.read(node.real_path) | ||
|
||
parser = Parser.new(contents, node.real_path.to_s) | ||
parser.parse | ||
|
||
parts = | ||
SemanticTokenizer.tokenize(parser.ast) | ||
|
||
mapped = | ||
parts.map do |item| | ||
case item | ||
in String | ||
"`#{skip { escape_for_javascript(item) }}`" | ||
in Tuple(SemanticTokenizer::TokenType, String) | ||
"_h('span', { className: '#{item[0].to_s.underscore}' }, [`#{skip { escape_for_javascript(item[1]) }}`])" | ||
end | ||
end | ||
|
||
"_h(React.Fragment, {}, [#{mapped.join(",\n")}])" | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
module Mint | ||
class Formatter | ||
def format(node : Ast::Directives::HighlightFile) | ||
"@highlight-file(#{node.path})" | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
module Mint | ||
class Parser | ||
def highlight_file_directive : Ast::Directives::HighlightFile? | ||
parse do |start_position| | ||
next unless word! "@highlight-file" | ||
whitespace | ||
|
||
next error :highlight_file_directive_expected_opening_parenthesis do | ||
expected "the opening parenthesis of an highlight file directive", word | ||
snippet self | ||
end unless char! '(' | ||
whitespace | ||
|
||
next error :highlight_file_directive_expected_path do | ||
expected "the path (to the file) of an highlight file directive", word | ||
snippet self | ||
end unless path = gather { chars { char != ')' } }.presence.try(&.strip) | ||
whitespace | ||
|
||
next error :highlight_file_directive_expected_closing_parenthesis do | ||
expected "the closing parenthesis of an highlight file directive", word | ||
snippet self | ||
end unless char! ')' | ||
|
||
Ast::Directives::HighlightFile.new( | ||
from: start_position, | ||
to: position, | ||
file: file, | ||
path: path) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
module Mint | ||
class TypeChecker | ||
def check(node : Ast::Directives::HighlightFile) : Checkable | ||
error! :highlight_file_directive_expected_file do | ||
block "The path specified for an highlight file directive does not exist: " | ||
|
||
if ENV["SPEC"]? | ||
snippet node.path.to_s | ||
else | ||
snippet node.real_path.to_s | ||
end | ||
|
||
snippet "The highlight file directive in question is here:", node | ||
end unless node.exists? | ||
|
||
contents = | ||
File.read(node.real_path) | ||
|
||
parser = Parser.new(contents, node.real_path.to_s) | ||
parser.parse | ||
parser.eof! | ||
|
||
error! :highlight_file_directive_expected_mint do | ||
block "I was expecting a Mint file for a highlight file directive " \ | ||
"but I could not parse it." | ||
|
||
snippet( | ||
"These are the first few lines of the file:", | ||
contents.lines[0..4].join("\n")) | ||
|
||
snippet "The highlight file directive in question is here:", node | ||
end unless parser.errors.empty? | ||
|
||
HTML | ||
end | ||
end | ||
end |