Skip to content

Commit

Permalink
feat(parser): support key combinations
Browse files Browse the repository at this point in the history
  • Loading branch information
MrMarble committed Apr 2, 2022
1 parent 5d48f69 commit 5720579
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 29 deletions.
2 changes: 2 additions & 0 deletions internal/lib/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,15 @@ func parseKeymap(file string) (*keymap.File, bool) {
log.Info().Msg("Parsing keymap file.")
r, err := os.Open(file)
if err != nil {
log.Err(err).Send()
return nil, false
}

ast, err := keymap.Parse(r)
defer r.Close()

if err != nil {
log.Err(err).Send()
return nil, false
}
return ast, true
Expand Down
8 changes: 7 additions & 1 deletion pkg/keymap/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type File struct {
Pos lexer.Position

Includes []*Include `parser:"@@+"`
Defines []*Define `parser:"@@*"`
Configs []*Config `parser:"@@"`
Device *Device `parser:"'/' '{' @@ '}'';'"`
}
Expand All @@ -25,6 +26,11 @@ type Include struct {

Value string `parser:"'#'Ident'<'@((Ident ('-' Ident)? '/'?)* ('.' Ident))'>'"`
}
type Define struct {
Pos lexer.Position

Value string `parser:"'#'Ident @Ident (Ident|Int)"`
}

type Config struct {
Pos lexer.Position
Expand Down Expand Up @@ -81,7 +87,7 @@ type Layer struct {

type List struct {
Number *int32 `parser:"@Int"`
KeyCode *string `parser:"| @(Ident ('('Ident')')?)"`
KeyCode *string `parser:"| @(Ident('('Ident('('Ident')')?')')?)"`
}

type Behavior struct {
Expand Down
56 changes: 28 additions & 28 deletions pkg/keymap/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ var keymapFile = `
&mt {
//flavor = "tap-preferred";
//tapping_term_ms = <200>;
flavor = "tap-preferred";
tapping_term_ms = <200>;
};
/ {
/ {
combos {
compatible = "zmk,combos";
Expand All @@ -25,81 +25,81 @@ var keymapFile = `
key-positions = <0 1>;
bindings = <&kp ESC>;
};
combo_tab {
timeout-ms = <50>;
key-positions = <10 11>;
bindings = <&kp TAB>;
};
combo_ralt {
timeout-ms = <50>;
key-positions = <17 16>;
bindings = <&kp RALT>;
};
combo_lalt {
timeout-ms = <50>;
key-positions = <11 12>;
bindings = <&kp LALT>;
};
combo_lgui {
timeout-ms = <50>;
key-positions = <12 13>;
bindings = <&kp LGUI>;
};
combo_rgui {
timeout-ms = <50>;
key-positions = <17 18>;
bindings = <&kp RGUI>;
};
};
keymap {
compatible = "zmk,keymap";
default_layer {
bindings = <
&kp Q &kp W &kp E &kp R &kp T &kp Y &kp U &kp I &kp O &kp P
&kp Q &kp W &kp E &kp R &kp T &kp Y &kp U &kp I &kp O &kp P
&kp A &kp S &kp D &kp F &kp G &kp H &kp J &kp K &kp L &kp QUOT
&mt LSFT Z &kp X &kp C &kp V &kp B &kp N &kp M &kp CMMA &kp DOT &mt LSFT RET
&mo 1 &kp LCTL &kp SPC &mo 2
&mo 1 &kp LCTL &kp SPC &mo 2
>;
};
left_layer {
bindings = <
&kp NUM_1 &kp NUM_2 &kp NUM_3 &kp NUM_4 &kp NUM_5 &kp NUM_6 &kp NUM_7 &kp NUM_8 &kp NUM_9 &kp NUM_0
&kp TAB &kp LC(S) &kp DQT &kp PIPE2 &kp HASH &kp MINUS &kp EQL &kp LBKT &kp RBKT &kp DEL
&kp ESC &kp TILDE &kp NON_US_BSLH &kp NON_US_HASH &kp TILDE2 &kp MINUS &kp GRAVE &kp LBKT &kp RBKT &kp DEL
&mo 1 &kp LGUI &kp RGUI &mo 2
&kp NUM_1 &kp NUM_2 &kp NUM_3 &kp NUM_4 &kp NUM_5 &kp NUM_6 &kp NUM_7 &kp NUM_8 &kp NUM_9 &kp NUM_0
&kp LC(LS(TAB)) &kp LC(S) &kp DQT &kp PIPE2 &kp HASH &kp MINUS &kp EQL &kp LBKT &kp RBKT &kp DEL
&kp ESC &kp TILDE &kp NON_US_BSLH &kp NON_US_HASH &kp TILDE2 &kp MINUS &kp GRAVE &kp LBKT &kp RBKT &kp DEL
&mo 1 &kp LGUI &kp RGUI &mo 2
>;
};
right_layer {
bindings = <
&kp BANG &kp ATSN &kp HASH &kp DLLR &kp PRCT &kp CRRT &kp AMPS &kp KMLT &kp LPRN &kp RPRN
&kp BANG &kp ATSN &kp HASH &kp DLLR &kp PRCT &kp CRRT &kp AMPS &kp KMLT &kp LPRN &kp RPRN
&kp HASH &kp QMARK &kp FSLH &kp COLN &kp SCLN &kp MINUS &kp KP_EQUAL &kp LBRC &kp RBRC &kp BKSP
&kp LSFT &kp KPLS &kp LBKT &kp RBKT &kp BSLH &kp UNDER &kp LEFT &kp DOWN &kp UP &kp RIGHT
&mo 3 &kp LCTL &kp SPC &mo 2
&mo 3 &kp LCTL &kp SPC &mo 2
>;
};
};
tri_layer {
bindings = <
&kp NUM_1 &kp NUM_2 &kp NUM_3 &kp NUM_4 &kp NUM_5 &trans &trans &trans &trans &trans
&kp F1 &kp F2 &kp F3 &kp F4 &kp F5 &trans &kp PG_UP &kp K_VOL_UP &kp K_MUTE &trans
&bt BT_CLR &bt BT_NXT &bt BT_PRV &kp F6 &kp F7 &trans &kp PG_DN &kp K_VOL_DN &trans &trans
&trans &trans &trans &trans
&kp F1 &kp F2 &kp F3 &kp F4 &kp F5 &trans &kp PG_UP &kp K_VOL_UP &kp K_MUTE &trans
&bt BT_CLR &bt BT_NXT &bt BT_PRV &kp F6 &kp F7 &trans &kp PG_DN &kp K_VOL_DN &trans &trans
&trans &trans &trans &trans
>;
};
};
};
};
`
Expand Down

0 comments on commit 5720579

Please sign in to comment.