traceSearch.pegjs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. {
  2. function as_undefined(){
  3. return undefined;
  4. }
  5. function as_null(){
  6. return null;
  7. }
  8. function as_operator_type(input){
  9. switch(input){
  10. case ">=":
  11. return "ge";
  12. case "<=":
  13. return "le";
  14. case "=":
  15. return "eq";
  16. case "<":
  17. return "lt";
  18. case ">":
  19. return "gt";
  20. }
  21. }
  22. function as_bool(input){
  23. switch(input){
  24. case "true": return true;
  25. case "false": return false;
  26. default: {
  27. throw new Error("Failed to parse boolean value")
  28. }
  29. }
  30. }
  31. }
  32. search_grammar
  33. = t:token* {
  34. return t
  35. }
  36. token = search
  37. search = s:search_key separator vo:value_operator? v:value {
  38. return {
  39. type: "Token",
  40. key: s.value,
  41. value: v,
  42. ...(s.negated ? {negated: true}: {}),
  43. ...(vo ? {operator: vo} : {}),
  44. }
  45. }
  46. search_key = space n:[!]? k:value space {
  47. return {
  48. value: k,
  49. ...(!!n ? {negated: true} : {}),
  50. }
  51. }
  52. value = space k:(bool/undefined/null/float/integer/string) space { return k }
  53. value_operator = space k:(">=" / "<=" / "=" / "<" / ">") space {
  54. return as_operator_type(text())
  55. }
  56. // Primitives
  57. bool = k:("true" / "false") { return as_bool(text()) }
  58. undefined = "undefined" { return as_undefined() }
  59. null = "null" { return as_null() }
  60. float = ([-])? ([0-9]+)? "." ([0-9]+)? !string {
  61. return parseFloat(text(), 10)
  62. }
  63. integer = ([-])?[0-9]+ !string{
  64. return parseInt(text(), 10)
  65. }
  66. string = [a-zA-Z0-9\-._!$]+ !bool !undefined !null {
  67. return text()
  68. }
  69. space = " "*
  70. separator = ":"
  71. eoi = [\t\n]