Skip to content

Commit

Permalink
feat(grammar): implement all of python's format specification
Browse files Browse the repository at this point in the history
  • Loading branch information
ValdezFOmar committed Oct 20, 2024
1 parent 832e4f0 commit d151d81
Show file tree
Hide file tree
Showing 4 changed files with 185 additions and 4 deletions.
13 changes: 13 additions & 0 deletions examples/format.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,17 @@ More {!a}
int: {0:d}; hex: {0:x}; oct: {0:o}; bin: {0:b}
{0.attr[ibute].hey}

To repr {var!r}
To string {!s}
To ascii {var!a}
Fill/Align {:<} {:>} {:=} {:^} {:-^} {:<<} {:>>} {:^=} {:=^}
Sign {:+} {:-} {: }
Special characters {:z} {:#} {:0}
Width {:10} {:48} {:01}
Grouping option {:,} {:_}
Float precision {:.2} {:.10}
Presentation types
{:b} {:c} {:d} {:e} {:E} {:f} {:F} {:g}
{:G} {:n} {:o} {:s} {:x} {:X} {:%}

# vim: ft=format_string
25 changes: 24 additions & 1 deletion grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@

const ALIGN_OPTIONS = ['<', '>', '=', '^'];
const SIGN_PATTERN = /[-+ ]/;
const DIGIT = /\d/;
const INTEGER = repeat1(DIGIT);
const TYPE_PATTERN = /[bcdeEfFgGnosxX%]/;
const GROUPING_OPTION_PATTERN = /[,_]/;

module.exports = grammar({
name: 'format_string',
Expand Down Expand Up @@ -44,13 +48,24 @@ module.exports = grammar({
':',
optional($.align),
optional($.sign),
optional('z'),
optional('#'),
optional($.width),
optional($.grouping_option),
optional($.precision),
optional($.type),
),

align: $ => seq(
field('fill', alias(
choice(
...ALIGN_OPTIONS,
SIGN_PATTERN,
'z',
'#',
DIGIT,
GROUPING_OPTION_PATTERN,
TYPE_PATTERN,
optional(/[^{}]/)
),
$.character
Expand All @@ -60,9 +75,17 @@ module.exports = grammar({

sign: _ => SIGN_PATTERN,

width: _ => INTEGER,

grouping_option: _ => GROUPING_OPTION_PATTERN,

precision: _ => token(seq('.', INTEGER)),

type: _ => TYPE_PATTERN,

// TODO: Python uses a wider range of characters for its identifiers
identifier: _ => /[a-zA-Z_][a-zA-Z0-9_]*/,
integer: _ => /[0-9]+/,
integer: _ => INTEGER,
item_string: _ => /[^\]]+/,
},
});
23 changes: 20 additions & 3 deletions queries/highlights.scm
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,31 @@
(attribute_expression
attribute: (identifier) @variable.member)

(integer) @number
[
(integer)
(width)
] @number

(precision) @number.float

(item_string) @string

(conversion) @function.macro

; Highlights for format spec...
; (format)
(type) @type

(character) @character

[
"<"
">"
"="
"^"
"z"
"#"
(sign)
(grouping_option)
] @character.special

[
"."
Expand Down
128 changes: 128 additions & 0 deletions test/corpus/main.txt
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,131 @@ Sign
(replacement
format: (format_spec
(sign))))

==================================================
Special characters
==================================================
{:z}
{:#}
{:0}
--------------------------------------------------
(text
(replacement
format: (format_spec))
(replacement
format: (format_spec))
(replacement
format: (format_spec
(width))))

==================================================
Width
==================================================
{:10}
{:48}
{:01}
--------------------------------------------------
(text
(replacement
format: (format_spec
(width)))
(replacement
format: (format_spec
(width)))
(replacement
format: (format_spec
(width))))

==================================================
Grouping option
==================================================
{:,}
{:_}
--------------------------------------------------
(text
(replacement
format: (format_spec
(grouping_option)))
(replacement
format: (format_spec
(grouping_option))))

==================================================
Float precision
==================================================
{:.2}
{:.10}
--------------------------------------------------
(text
(replacement
format: (format_spec
(precision)))
(replacement
format: (format_spec
(precision))))

==================================================
Presentation types
==================================================
{:b}
{:c}
{:d}
{:e}
{:E}
{:f}
{:F}
{:g}
{:G}
{:n}
{:o}
{:s}
{:x}
{:X}
{:%}
--------------------------------------------------
(text
(replacement
format: (format_spec
(type)))
(replacement
format: (format_spec
(type)))
(replacement
format: (format_spec
(type)))
(replacement
format: (format_spec
(type)))
(replacement
format: (format_spec
(type)))
(replacement
format: (format_spec
(type)))
(replacement
format: (format_spec
(type)))
(replacement
format: (format_spec
(type)))
(replacement
format: (format_spec
(type)))
(replacement
format: (format_spec
(type)))
(replacement
format: (format_spec
(type)))
(replacement
format: (format_spec
(type)))
(replacement
format: (format_spec
(type)))
(replacement
format: (format_spec
(type)))
(replacement
format: (format_spec
(type))))

0 comments on commit d151d81

Please sign in to comment.