{"action":"created","issue":{"url":"https://api.github.com/repos/cdglabs/ohm/issues/99","repository_url":"https://api.github.com/repos/cdglabs/ohm","labels_url":"https://api.github.com/repos/cdglabs/ohm/issues/99/labels{/name}","comments_url":"https://api.github.com/repos/cdglabs/ohm/issues/99/comments","events_url":"https://api.github.com/repos/cdglabs/ohm/issues/99/events","html_url":"https://github.com/cdglabs/ohm/issues/99","id":153081861,"number":99,"title":"How do I extend the semantics of an inherited grammar?","user":{"login":"justinmchase","id":10974,"avatar_url":"https://avatars.githubusercontent.com/u/10974?v=3","gravatar_id":"","url":"https://api.github.com/users/justinmchase","html_url":"https://github.com/justinmchase","followers_url":"https://api.github.com/users/justinmchase/followers","following_url":"https://api.github.com/users/justinmchase/following{/other_user}","gists_url":"https://api.github.com/users/justinmchase/gists{/gist_id}","starred_url":"https://api.github.com/users/justinmchase/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/justinmchase/subscriptions","organizations_url":"https://api.github.com/users/justinmchase/orgs","repos_url":"https://api.github.com/users/justinmchase/repos","events_url":"https://api.github.com/users/justinmchase/events{/privacy}","received_events_url":"https://api.github.com/users/justinmchase/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2016-05-04T18:25:02Z","updated_at":"2016-05-04T18:41:36Z","closed_at":"2016-05-04T18:41:36Z","body":"What I'm trying to do is to extend the es5 grammar and add some custom semantics.\r\n\r\nFirst off I am able to essentially eval the es5 code like this:\r\n```\r\nvar ohm = require('ohm-js')\r\nvar es5 = require('ohm-js/examples/ecmascript/es5')\r\nvar code = '2 + 3'\r\nvar grammar = es5.grammar\r\nvar match = grammar.match(code)\r\nvar semantics = es5.semantics\r\nvar result = semantics(match)\r\nassert(eval(result.asES5) === 5)\r\n```\r\n\r\nThis appears to actually work. But what I would like to do is to extend es5 and add my own semantics which then allow me to expand those semantics into javascript.\r\n\r\nFor example suppose I wrote my own grammar like this:\r\n```\r\nExample <: ES5 {\r\n  example = \"example\"\r\n  keyword += example\r\n  Declaration += ExampleDeclaration\r\n  ExampleDeclaration = \"example\" #(sc)\r\n}\r\n```\r\n\r\nNow, I modify my original code to be more like this:\r\n```\r\nvar ohm = require('ohm-js')\r\nvar es5 = require('ohm-js/examples/ecmascript/es5')\r\nvar code = 'example;'\r\nvar grammar = ohm.grammar(fs.readFileSync('./g.ohm'), { ES5:  es5.grammar })\r\nvar match = grammar.match(code)\r\n\r\nvar semantics = // ??\r\n\r\nvar result = semantics(match)\r\nassert(eval(result.asES5) === 5)\r\n```\r\n\r\nWhat I would like to do, for this example, is to essentially transform the matched `ExampleDeclaration` into `2 + 3`. How can I use the es5 semantics object but modify it to do custom stuff just for my matched semantics?"},"comment":{"url":"https://api.github.com/repos/cdglabs/ohm/issues/comments/216961189","html_url":"https://github.com/cdglabs/ohm/issues/99#issuecomment-216961189","issue_url":"https://api.github.com/repos/cdglabs/ohm/issues/99","id":216961189,"user":{"login":"alexwarth","id":247691,"avatar_url":"https://avatars.githubusercontent.com/u/247691?v=3","gravatar_id":"","url":"https://api.github.com/users/alexwarth","html_url":"https://github.com/alexwarth","followers_url":"https://api.github.com/users/alexwarth/followers","following_url":"https://api.github.com/users/alexwarth/following{/other_user}","gists_url":"https://api.github.com/users/alexwarth/gists{/gist_id}","starred_url":"https://api.github.com/users/alexwarth/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/alexwarth/subscriptions","organizations_url":"https://api.github.com/users/alexwarth/orgs","repos_url":"https://api.github.com/users/alexwarth/repos","events_url":"https://api.github.com/users/alexwarth/events{/privacy}","received_events_url":"https://api.github.com/users/alexwarth/received_events","type":"User","site_admin":false},"created_at":"2016-05-04T18:41:36Z","updated_at":"2016-05-04T18:41:36Z","body":"Here's how you'd create a semantics for your `Example` grammar that inherits from the semantics that you created for the `ES5` grammar:\r\n\r\n```\r\nvar semantics = grammar.extendSemantics(es5.semantics);\r\n```\r\n\r\nYou can extend / override the semantic actions of the operations and attributes that `semantics` inherited from its super-semantics using the `extendOperation` and `extendAttribute` methods, respectively. Here's a link to the relevant part of the documentation: https://github.com/cdglabs/ohm/blob/master/doc/api-reference.md#user-content-semantics"}}