Skip to content

Instantly share code, notes, and snippets.

@zodoz
zodoz / gist:5152201
Created March 13, 2013 13:46
Useful PEGjs piece that can allow optional parts of a grammer to occur without a required order and be arrayed. Thus you can have only one block of "a"s, "b"s, and "c"s in any order, but as many blocks of "x"s, "y"s, and "z"s again in any order.
{var aDone = bDone = cDone = false;}
start = part part?
part = sets:set*
{
var ret = {};
for(var i=0;i<sets.length;i++) {
// add to set if it already exists
if(ret[sets[i].type] !== undefined) {
{
function styleProps() {
// size : style / config
return ['className', 'size', 'width', 'disabledLabel', 'textAlign', 'marginRight', 'marginLeft', 'marginTop', 'marginBottom', 'contentSpan'];
}
function configProps() {
return ['htmlType', 'dataSource', 'button', 'value', 'defaultValue', 'placeholder', 'type', 'html', 'autoWidth', 'max', 'min', 'addButtonLabel', 'indexFormat', 'mode', 'data-repeater', 'shape', 'previewHidden'];
}
function ruleProps() {
return ['rule', 'showPath'];
function factorialIterative(num) {
var result = 1;
for (var i=num; i>0; i--)
result *= i;
return result;
}
function factorial(num) {
if (num == 0)
return 1;
//option 1: define as a object
var myFn = {
duplicate: function(str, num) {
num = num || 2;
return (new Array(num + 1)).join(str);
},
reverse: function(str) {
return str.split("").reverse().join("");
},
chop: function(str, index, len) {
@Siilwyn
Siilwyn / if.js
Last active April 26, 2024 08:05
Multiple ways to handle the platform: switch vs. if...else vs. object
var getConfigDirectory = function () {
var platform = process.platform;
if (platform === 'linux') {
return process.env.XDG_CONFIG_HOME || path.join(home(), '.config')
}
else if (platform === 'darwin') {
return path.join(home(), 'Library', 'Preferences');
}
else if (platform === 'win32') {
//https://pegjs.org/online
start = table / trx
table = "table" space s:schema "." t:tablename ":" space a:action ":" d:data {return {schema:s, table:t, action:a, data:d};}
trx = trx_begin / trx_commit
trx_begin = "BEGIN" id:trx_id? {return {trx:"BEGIN", xid:id};}
trx_commit = "COMMIT" id:trx_id? tm:trx_tm {return {trx:"COMMIT", xid:id, time:tm};}
@afucher
afucher / iniFileParser.js
Created February 4, 2016 00:25
Grammar from PEGjs for ini file
IniFile
= first: Section (BlankLine* others: Section*) {others.unshift(first); return others; }
Section
= section: SectionHeader "\n" values: KeyValues* {return {section:values}}
SectionHeader
= "[" section_name: word "]" {return section_name}
KeyValues
= a: word "=" value: word "\n"? {var o = {}; o[a]=value; return o;}
@mytharcher
mytharcher / README.md
Last active April 26, 2024 08:04
Grammar Descriptions in PEG.js

Elimination of left recursion

C -> D o D
D -> C
D -> t

D -> D o D
D -> t
@netgusto
netgusto / pegjs-htmlish.peg
Last active April 26, 2024 08:04
PEG.js grammar for parsing simple HTML-ish balanced markup language - use it on http://pegjs.majda.cz/online
Content = (DocType / Comment / BalancedTag / SelfClosingTag / Text)*
DocType = "<!doctype " doctype:[^>]* ">" {
return {
type: 'DocType',
content: doctype.join('')
};
}
Comment = "<!--" c:(!"-->" c:. {return c})* "-->" {
@axelbdt
axelbdt / django-postgresql-15.md
Last active April 26, 2024 08:03
# Django and PostgreSQL 15, the rules have changed

Django and PostgreSQL 15, the rules have changed

Postgres 15 is just out, and while there is a lot to love about this new release, you're in for a surprise if you try to set it up with Django following tutorials like this one.

The reason is stated in the release announcement:

Remove PUBLIC creation permission on the public schema (Noah Misch) The new default is one of the secure schema usage patterns that Section 5.9.6 has recommended...

Provided your web app doesn't access your database as a superuser (it shouldn't) and uses a dedicated user, it is not allowed to use the public schema anymore. You have to create one for this specific user, and the next section will