osclang.js 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. this.ohmLang_get = function () {
  2. return this.ohmLang;
  3. }
  4. this.ohmLang_set = function (val) {
  5. if (val == "grammar") {
  6. this.ohmLang = this.ohmLang_grammar();
  7. } else {
  8. this.ohmLang = val;
  9. }
  10. }
  11. this.ohmLang_grammar = function () {
  12. return 'parseOSC { \n' +
  13. 'all = address ":" props \n' +
  14. 'address = ("/" addr)* \n' +
  15. 'addr = ~("/") propSingle \n' +
  16. 'props \n' +
  17. ' = propSingle row -- single \n' +
  18. ' | "rgb" row -- rgb \n' +
  19. ' | propSingle number -- prop \n' +
  20. 'row = "[" col rep "]" \n' +
  21. 'rep = ("," col)* \n' +
  22. 'col = colChar* \n' +
  23. 'colChar = ~("[" | "," | "]") number \n' +
  24. 'propSingle = ~("rgb") letter* \n' +
  25. 'number (a number) \n' +
  26. ' = digit* "." digit+ -- fract \n' +
  27. ' | digit+ -- whole \n' +
  28. '}'
  29. }
  30. this.initLang = function () {
  31. console.log("add operations to semantics")
  32. this.addOperationLang();
  33. }
  34. this.addOperationLang = function () {
  35. var self = this;
  36. this.semantics.addOperation('parse',
  37. {
  38. all: function (e, _, k) { return { "address": e.parse(), "params": k.parse() } },
  39. address: function (_, e) { return e.parse() },
  40. addr: function (e) { return e.parse() },
  41. props: function (e) { return e.parse() },
  42. props_single: function (e, k) {
  43. return { 'propName': e.parse(), 'propValue': k.parse() };
  44. },
  45. props_rgb: function (_, e) {
  46. return { 'propName': 'color', 'propValue': ['rgb(' + e.parse() + ')'] };
  47. },
  48. props_prop: function (e, k) {
  49. return { 'propName': e.parse(), 'propValue': k.parse() };
  50. },
  51. row: function (_l, e, k, _e) {
  52. let end = k.parse();
  53. if (end.length !== 0) {
  54. return e.parse() + ',' + k.parse();
  55. }
  56. return e.parse()
  57. },
  58. rep: function (_, e) { return e.parse() },
  59. col: function (e) { return e.parse() },
  60. colChar: function (e) { return e.parse() },
  61. number: function (_) { return parseFloat(this.sourceString) },
  62. propSingle: function (_) { return this.sourceString }
  63. });
  64. }
  65. this.getOSC = function (msg) {
  66. this.parseOSC(msg);
  67. }
  68. this.parseOSC = function (msg) {
  69. let str = msg.address + JSON.stringify(msg.args);
  70. var match = this.grammar.match(str, "all");
  71. if (match.succeeded()) {
  72. let res = this.semantics(match).parse();
  73. this.setPropsFromOSC(res);
  74. }
  75. }
  76. this.setPropsFromOSC = function (res) {
  77. //console.log(res);
  78. let address = '/' + res.address.join('/');
  79. let nodeID = vwf.find("", address);
  80. if (res.params.propValue.length == 1) {
  81. vwf_view.kernel.setProperty(nodeID, res.params.propName, [res.params.propValue[0]])
  82. }
  83. if (res.params.propValue.length >= 1) {
  84. vwf_view.kernel.setProperty(nodeID, res.params.propName, [res.params.propValue])
  85. }
  86. }