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

Add support for constants #8

Merged
merged 3 commits into from
Jun 19, 2024
Merged
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
22 changes: 21 additions & 1 deletion grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ module.exports = grammar({
$.import,
$.type_alias,
$.type_struct,
$.type_enum
$.type_enum,
$.constant,
),

// Handles import definitions
Expand Down Expand Up @@ -74,6 +75,25 @@ module.exports = grammar({
seq($.type_identifier, optional(seq("<", repeat_separated_by($.type_argument, ","), ">"))),
type_argument: ($) => field("type_argument", choice($.identifier, $.type_definition)),

// Constants
constant: ($) => seq("const", $.identifier, optional(seq(":", $.type_definition)), "=", $.constant_value),
constant_value: ($) => choice(
$.int,
$.string,
$.bytes,
// $.curvepoint - Should this be here?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can do this later

),

base10: (_$) => token(/[0-9]+/),
base10_underscore: (_$) => token(/[0-9]+(_[0-9]+)+/),
base16: (_$) => token(/0x[0-9a-fA-F]+/),
int: ($) => choice($.base10, $.base10_underscore, $.base16),

string: ($) => seq("@", $.string_inner),
bytes: ($) => seq(optional("#"), $.string_inner),
string_inner: ($) => seq('"', repeat(choice(/[^\\"]/, $.escape)), '"'),
escape: ($) => token(/\\./),

// Comments
module_comment: (_$) => token(seq("////", /.*/)),
definition_comment: (_$) => token(seq("///", /.*/)),
Expand Down
149 changes: 149 additions & 0 deletions test/corpus/constants.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
================================================================================
Constant Int Base10 w/ Type
================================================================================

const name: Int = 100

--------------------------------------------------------------------------------

(source_file
(constant (identifier)
(type_definition (type_identifier))
(constant_value (int (base10)))))

================================================================================
Constant Int Base10 w/o Type
================================================================================

const name = 100

--------------------------------------------------------------------------------

(source_file
(constant (identifier)
(constant_value (int (base10)))))

================================================================================
Constant Int Base16 w/ Type
================================================================================

const name: Int = 0xaa

--------------------------------------------------------------------------------

(source_file
(constant (identifier)
(type_definition (type_identifier))
(constant_value (int (base16)))))

================================================================================
Constant Int Base16 w/o Type
================================================================================

const name = 0xaa

--------------------------------------------------------------------------------

(source_file
(constant (identifier)
(constant_value (int (base16)))))

================================================================================
Constant Int base10_underscore w/ Type
================================================================================

const name: Int = 1_000_000

--------------------------------------------------------------------------------

(source_file
(constant (identifier)
(type_definition (type_identifier))
(constant_value (int (base10_underscore)))))

================================================================================
Constant Int base10_underscore w/o Type
================================================================================

const name = 1_000_000

--------------------------------------------------------------------------------

(source_file
(constant (identifier)
(constant_value (int (base10_underscore)))))

================================================================================
Constant String w/ Type
================================================================================

const name: String = @"ilikecardano"

--------------------------------------------------------------------------------

(source_file
(constant (identifier)
(type_definition (type_identifier))
(constant_value (string (string_inner)))))

================================================================================
Constant String w/o Type
================================================================================

const name = @"ilikecardano"

--------------------------------------------------------------------------------

(source_file
(constant (identifier)
(constant_value (string (string_inner)))))

================================================================================
Constant Bytes w/ Type + #
================================================================================

const name: Bytes = #"deadbeef"

--------------------------------------------------------------------------------

(source_file
(constant (identifier)
(type_definition (type_identifier))
(constant_value (bytes (string_inner)))))

================================================================================
Constant Bytes w/o Type + #
================================================================================

const name = #"deadbeef"

--------------------------------------------------------------------------------

(source_file
(constant (identifier)
(constant_value (bytes (string_inner)))))

================================================================================
Constant Bytes w/ Type
================================================================================

const name: Bytes = "deadbeef"

--------------------------------------------------------------------------------

(source_file
(constant (identifier)
(type_definition (type_identifier))
(constant_value (bytes (string_inner)))))

================================================================================
Constant Bytes w/o Type
================================================================================

const name = "deadbeef"

--------------------------------------------------------------------------------

(source_file
(constant (identifier)
(constant_value (bytes (string_inner)))))
Loading