filter parser updates?

This commit is contained in:
Anish Lakhwara 2025-02-19 23:17:41 -08:00
parent 20e50346f4
commit 2d53428f56
3 changed files with 103 additions and 0 deletions

18
parser/command_parser.d.ts vendored Normal file
View file

@ -0,0 +1,18 @@
export interface Part {
type: 'text';
value: string;
reconstruct: () => string;
}
export interface Command {
type: 'add' | 'filter' | 'done';
description: string;
attributes: string[];
filters: string[];
project: string;
tags: string;
parts: Part[];
reconstruct: () => string;
}
export function parse(input: string): Command;

54
parser/filter_go.peg Normal file
View file

@ -0,0 +1,54 @@
{
package parser
type IdFilter struct {
Ids []int
}
}
Filter <- first:(IdRange / SingleId) rest:(',' (IdRange / SingleId))* {
var ids []int
first := toIntSlice(first)
ids = append(ids, first...)
if rest != nil {
for _, r := range rest.([]interface{}) {
restIds := toIntSlice(r.([]interface{})[1])
ids = append(ids, restIds...)
}
}
return &IdFilter{Ids: ids}, nil
}
IdRange <- start:Integer '-' end:Integer {
start := start.(int)
end := end.(int)
var ids []int
for i := start; i <= end; i++ {
ids = append(ids, i)
}
return ids, nil
}
SingleId <- id:Integer {
return []int{id.(int)}, nil
}
Integer <- [0-9]+ {
val := string(c.text)
n, _ := strconv.Atoi(val)
return n, nil
}
_ <- [ \t]*
{
func toIntSlice(v interface{}) []int {
if slice, ok := v.([]int); ok {
return slice
}
return nil
}
}

31
parser/filter_js.peg Normal file
View file

@ -0,0 +1,31 @@
{
function makeIdFilter(ids) {
return {
type: 'id_filter',
ids: ids.flat(),
reconstruct: function() {
return this.ids.join(',');
}
};
}
}
Filter = first:(IdRange / SingleId) rest:("," (IdRange / SingleId))* {
return makeIdFilter([first, ...rest.map(r => r[1])]);
}
IdRange = start:Integer "-" end:Integer {
const ids = [];
for (let i = start; i <= end; i++) {
ids.push(i);
}
return ids;
}
SingleId = id:Integer {
return [id];
}
Integer = digits:[0-9]+ {
return parseInt(digits.join(''), 10);
}