SlideShare a Scribd company logo
HTML Output
<article>
<h1>Title</h1>
<p>Body</p>
</article>
Pug Source
article
h1 Title
p Body
Pug Compiler
Lexer Parser Code Gen
Lexer
article
h1 Title
p Body
• tag – "article"
• indent
• tag – "h1"
• text – "Title"
• new line
• tag – "p"
• text – "Body"
• outdent
Lexer
class Lexer {
constructor(str) {
this.str = str;
this.indent = 0;
}
}
Lexer
tag() {
let match = /^[a-z]+/.exec(this.str);
if (match) {
this.str = this.str.substr(match[0].length);
return {type: 'tag', val: match[0]};
}
}
Lexer
text() {
let match = /^ .+/.exec(this.str);
if (match) {
this.str = this.str.substr(match[0].length);
return {type: 'text', val: match[0].substr(1)};
}
}
Lexer
newline() {
let match = /^n( *)/.exec(this.str);
if (match) {
this.str = this.str.substr(match[0].length);
let oldIndent = this.indent;
this.indent = match[1].length;
if (this.indent > oldIndent) return {type: 'indent'};
if (this.indent < oldIndent) return {type: 'outdent'};
return {type: 'newline'};
}
}
Lexer
fail() {
throw new Error(
'Unexpected text'
);
}
getTokens() {
let tokens = [];
while (this.str.length) {
tokens.push(
this.newline() ||
this.tag() ||
this.text() ||
this.fail()
);
}
tokens.push({type: 'eos'});
return tokens;
}
Parser
OutdentIndent
New
Line
Text
"Body"
Tag
"h1"
Tag
"article"
Text
"Title"
Tag
"p"
Parser
Text
"Body"
Tag
"h1"
Tag
"article"
Text
"Title"
Tag
"p"
Parser
class Parser {
constructor(tokens) {
this.tokens = new TokenStream(tokens);
}
}
Token Stream
• Next – get the next token and advance the stream
• Peek – get the next token without advancing the stream
• Expect(type) – get the next token and advance the stream
Parser
parseFile() {
let nodes = [];
while (this.tokens.peek().type !== 'eos') {
if (this.tokens.peek().type === 'tag') {
nodes.push(this.parseTag());
} else {
this.tokens.expect('newline');
}
}
this.tokens.expect('eos');
return {type: 'File', nodes};
}
Parser
parseTag() {
let tok = this.tokens.expect('tag');
let body = null;
switch (this.tokens.peek().type) {
case 'text':
body = this.parseText();
break;
case 'indent':
body = this.parseBlock();
break;
}
return {type: 'Tag', name: tok.val, body};
}
Parser
parseBlock() {
this.tokens.expect('indent');
let nodes = [];
while (this.tokens.peek().type !== 'outdent') {
if (this.tokens.peek().type === 'tag') {
nodes.push(this.parseTag());
} else {
this.tokens.expect('newline');
}
}
this.tokens.expect('outdent');
return {type: 'Block', nodes};
}
Code Gen
Text
"Body"
Tag
"h1"
Tag
"article"
Text
"Title"
Tag
"p"
Code Gen
Text
"Body"
Tag
"h1"
Tag
"article"
"Title"
Tag
"p"
Code Gen
Text
"Body"
"<h1>Title</h1>"
Tag
"article"
Tag
"p"
Code Gen
"Body"
"<h1>Title</h1>"
Tag
"article"
Tag
"p"
Code Gen
"<h1>Title</h1>"
Tag
"article"
"<p>Body</p>"
Code Gen "<article><h1>Title</h1><p>Body</p></article>"
Code Gen
function render(node) {
switch (node.type) {
case 'Block':
return renderBlock(node);
case 'Tag':
return renderTag(node);
case 'Text':
return renderText(node);
}
}
Code Gen
function renderBlock(node) {
return node.nodes.map(render).join('n');
}
Code Gen
function renderTag(node) {
if (!node.body) return '<' + node.name + '/>';
return (
'<' + node.name + '>' +
render(node.body) +
'</' + node.name + '>'
);
}
Code Gen
function renderText(node) {
return node.val;
}
Pug Compiler
Lexer Parser Code Gen
Includes
article
h1 Title
include ./content.pug
Linking
File A
Include
File B
FILE B
Linking
File A
Include
File B
FILE B
Pug Compiler Pipeline
Lexer Parser Loader Linker Code-Gen
String Tokens AST Collection
of ASTs
AST String
Lexer Parser Loader Linker Code-Gen
Now it's your turn!
String Tokens AST Collection
of ASTs
AST String

More Related Content

What's hot

The Ring programming language version 1.6 book - Part 42 of 189
The Ring programming language version 1.6 book - Part 42 of 189The Ring programming language version 1.6 book - Part 42 of 189
The Ring programming language version 1.6 book - Part 42 of 189
Mahmoud Samir Fayed
 
Crawler 2
Crawler 2Crawler 2
Crawler 2
Cheng-Yi Yu
 
jSession #4 - Maciej Puchalski - Zaawansowany retrofit
jSession #4 - Maciej Puchalski - Zaawansowany retrofitjSession #4 - Maciej Puchalski - Zaawansowany retrofit
jSession #4 - Maciej Puchalski - Zaawansowany retrofit
jSession
 
Lean & Mean Tokyo Cabinet Recipes (with Lua) - FutureRuby '09
Lean & Mean Tokyo Cabinet Recipes (with Lua) - FutureRuby '09Lean & Mean Tokyo Cabinet Recipes (with Lua) - FutureRuby '09
Lean & Mean Tokyo Cabinet Recipes (with Lua) - FutureRuby '09
Ilya Grigorik
 
Linux intro 4 awk + makefile
Linux intro 4  awk + makefileLinux intro 4  awk + makefile
Linux intro 4 awk + makefile
Giovanni Marco Dall'Olio
 
Full Text Search in PostgreSQL
Full Text Search in PostgreSQLFull Text Search in PostgreSQL
Full Text Search in PostgreSQL
Aleksander Alekseev
 
Meet.js promises
Meet.js promisesMeet.js promises
Meet.js promises
Krzysztof Kula
 
using python module: doctest
using python module: doctestusing python module: doctest
using python module: doctest
mitnk
 
2015 555 kharchenko_ppt
2015 555 kharchenko_ppt2015 555 kharchenko_ppt
2015 555 kharchenko_ppt
Maxym Kharchenko
 
Better Full Text Search in PostgreSQL
Better Full Text Search in PostgreSQLBetter Full Text Search in PostgreSQL
Better Full Text Search in PostgreSQL
Artur Zakirov
 
Implementing pseudo-keywords through Functional Programing
Implementing pseudo-keywords through Functional ProgramingImplementing pseudo-keywords through Functional Programing
Implementing pseudo-keywords through Functional Programing
Vincent Pradeilles
 
Drinking from the Elixir Fountain of Resilience
Drinking from the Elixir Fountain of ResilienceDrinking from the Elixir Fountain of Resilience
Drinking from the Elixir Fountain of Resilience
C4Media
 
Git Tutorial Yang Yang
Git Tutorial Yang YangGit Tutorial Yang Yang
Git Tutorial Yang Yang
Yang Yang
 
Productive Programming in Groovy
Productive Programming in GroovyProductive Programming in Groovy
Productive Programming in Groovy
Ganesh Samarthyam
 
Pdxpugday2010 pg90
Pdxpugday2010 pg90Pdxpugday2010 pg90
Pdxpugday2010 pg90
Selena Deckelmann
 
Logic Equations Resolver J Script
Logic Equations Resolver   J ScriptLogic Equations Resolver   J Script
Logic Equations Resolver J Script
Roman Agaev
 
Rest in Good! - DevCon 2017
Rest in Good! - DevCon 2017Rest in Good! - DevCon 2017
Rest in Good! - DevCon 2017
André Goliath
 

What's hot (17)

The Ring programming language version 1.6 book - Part 42 of 189
The Ring programming language version 1.6 book - Part 42 of 189The Ring programming language version 1.6 book - Part 42 of 189
The Ring programming language version 1.6 book - Part 42 of 189
 
Crawler 2
Crawler 2Crawler 2
Crawler 2
 
jSession #4 - Maciej Puchalski - Zaawansowany retrofit
jSession #4 - Maciej Puchalski - Zaawansowany retrofitjSession #4 - Maciej Puchalski - Zaawansowany retrofit
jSession #4 - Maciej Puchalski - Zaawansowany retrofit
 
Lean & Mean Tokyo Cabinet Recipes (with Lua) - FutureRuby '09
Lean & Mean Tokyo Cabinet Recipes (with Lua) - FutureRuby '09Lean & Mean Tokyo Cabinet Recipes (with Lua) - FutureRuby '09
Lean & Mean Tokyo Cabinet Recipes (with Lua) - FutureRuby '09
 
Linux intro 4 awk + makefile
Linux intro 4  awk + makefileLinux intro 4  awk + makefile
Linux intro 4 awk + makefile
 
Full Text Search in PostgreSQL
Full Text Search in PostgreSQLFull Text Search in PostgreSQL
Full Text Search in PostgreSQL
 
Meet.js promises
Meet.js promisesMeet.js promises
Meet.js promises
 
using python module: doctest
using python module: doctestusing python module: doctest
using python module: doctest
 
2015 555 kharchenko_ppt
2015 555 kharchenko_ppt2015 555 kharchenko_ppt
2015 555 kharchenko_ppt
 
Better Full Text Search in PostgreSQL
Better Full Text Search in PostgreSQLBetter Full Text Search in PostgreSQL
Better Full Text Search in PostgreSQL
 
Implementing pseudo-keywords through Functional Programing
Implementing pseudo-keywords through Functional ProgramingImplementing pseudo-keywords through Functional Programing
Implementing pseudo-keywords through Functional Programing
 
Drinking from the Elixir Fountain of Resilience
Drinking from the Elixir Fountain of ResilienceDrinking from the Elixir Fountain of Resilience
Drinking from the Elixir Fountain of Resilience
 
Git Tutorial Yang Yang
Git Tutorial Yang YangGit Tutorial Yang Yang
Git Tutorial Yang Yang
 
Productive Programming in Groovy
Productive Programming in GroovyProductive Programming in Groovy
Productive Programming in Groovy
 
Pdxpugday2010 pg90
Pdxpugday2010 pg90Pdxpugday2010 pg90
Pdxpugday2010 pg90
 
Logic Equations Resolver J Script
Logic Equations Resolver   J ScriptLogic Equations Resolver   J Script
Logic Equations Resolver J Script
 
Rest in Good! - DevCon 2017
Rest in Good! - DevCon 2017Rest in Good! - DevCon 2017
Rest in Good! - DevCon 2017
 

Similar to Pug - a compiler pipeline

webScrapingFunctions
webScrapingFunctionswebScrapingFunctions
webScrapingFunctions
Hellen Gakuruh
 
Javascripting.pptx
Javascripting.pptxJavascripting.pptx
Javascripting.pptx
Vinod Srivastava
 
Json
JsonJson
Javascript2839
Javascript2839Javascript2839
Javascript2839
Ramamohan Chokkam
 
Cypher inside out: Como a linguagem de pesquisas em grafo do Neo4j foi constr...
Cypher inside out: Como a linguagem de pesquisas em grafo do Neo4j foi constr...Cypher inside out: Como a linguagem de pesquisas em grafo do Neo4j foi constr...
Cypher inside out: Como a linguagem de pesquisas em grafo do Neo4j foi constr...
adrianoalmeida7
 
Unfiltered Unveiled
Unfiltered UnveiledUnfiltered Unveiled
Unfiltered Unveiled
Wilfred Springer
 
WOTC_Import
WOTC_ImportWOTC_Import
WOTC_Import
Luther Quinn
 
Spark with Elasticsearch
Spark with ElasticsearchSpark with Elasticsearch
Spark with Elasticsearch
Holden Karau
 
Javascript built in String Functions
Javascript built in String FunctionsJavascript built in String Functions
Javascript built in String Functions
Avanitrambadiya
 
Perl6 Regexen: Reduce the line noise in your code.
Perl6 Regexen: Reduce the line noise in your code.Perl6 Regexen: Reduce the line noise in your code.
Perl6 Regexen: Reduce the line noise in your code.
Workhorse Computing
 
Linq introduction
Linq introductionLinq introduction
Linq introduction
Mohammad Alyan
 
Scala 2 + 2 > 4
Scala 2 + 2 > 4Scala 2 + 2 > 4
Scala 2 + 2 > 4
Emil Vladev
 
EmberConf 2021 - Crossfile Codemodding with Joshua Lawrence
EmberConf 2021 - Crossfile Codemodding with Joshua LawrenceEmberConf 2021 - Crossfile Codemodding with Joshua Lawrence
EmberConf 2021 - Crossfile Codemodding with Joshua Lawrence
Joshua Lawrence
 
Postgresql 9.3 overview
Postgresql 9.3 overviewPostgresql 9.3 overview
Postgresql 9.3 overview
Aveic
 
Grammarware Memes
Grammarware MemesGrammarware Memes
Grammarware Memes
Eelco Visser
 
An Annotation Framework for Statically-Typed Syntax Trees
An Annotation Framework for Statically-Typed Syntax TreesAn Annotation Framework for Statically-Typed Syntax Trees
An Annotation Framework for Statically-Typed Syntax Trees
Ray Toal
 
Binary Studio Academy PRO: ANTLR course by Alexander Vasiltsov (lesson 4)
Binary Studio Academy PRO: ANTLR course by Alexander Vasiltsov (lesson 4)Binary Studio Academy PRO: ANTLR course by Alexander Vasiltsov (lesson 4)
Binary Studio Academy PRO: ANTLR course by Alexander Vasiltsov (lesson 4)
Binary Studio
 
FNT 2015 PDIS CodeEU - Zanimljiva informatika - 02 Djordje Pavlovic - Live_ch...
FNT 2015 PDIS CodeEU - Zanimljiva informatika - 02 Djordje Pavlovic - Live_ch...FNT 2015 PDIS CodeEU - Zanimljiva informatika - 02 Djordje Pavlovic - Live_ch...
FNT 2015 PDIS CodeEU - Zanimljiva informatika - 02 Djordje Pavlovic - Live_ch...
Педагошко друштво информатичара Србије
 
Ast transformations
Ast transformationsAst transformations
Ast transformations
HamletDRC
 
#include iostream #include cstring #include vector #i.pdf
 #include iostream #include cstring #include vector #i.pdf #include iostream #include cstring #include vector #i.pdf
#include iostream #include cstring #include vector #i.pdf
anandatalapatra
 

Similar to Pug - a compiler pipeline (20)

webScrapingFunctions
webScrapingFunctionswebScrapingFunctions
webScrapingFunctions
 
Javascripting.pptx
Javascripting.pptxJavascripting.pptx
Javascripting.pptx
 
Json
JsonJson
Json
 
Javascript2839
Javascript2839Javascript2839
Javascript2839
 
Cypher inside out: Como a linguagem de pesquisas em grafo do Neo4j foi constr...
Cypher inside out: Como a linguagem de pesquisas em grafo do Neo4j foi constr...Cypher inside out: Como a linguagem de pesquisas em grafo do Neo4j foi constr...
Cypher inside out: Como a linguagem de pesquisas em grafo do Neo4j foi constr...
 
Unfiltered Unveiled
Unfiltered UnveiledUnfiltered Unveiled
Unfiltered Unveiled
 
WOTC_Import
WOTC_ImportWOTC_Import
WOTC_Import
 
Spark with Elasticsearch
Spark with ElasticsearchSpark with Elasticsearch
Spark with Elasticsearch
 
Javascript built in String Functions
Javascript built in String FunctionsJavascript built in String Functions
Javascript built in String Functions
 
Perl6 Regexen: Reduce the line noise in your code.
Perl6 Regexen: Reduce the line noise in your code.Perl6 Regexen: Reduce the line noise in your code.
Perl6 Regexen: Reduce the line noise in your code.
 
Linq introduction
Linq introductionLinq introduction
Linq introduction
 
Scala 2 + 2 > 4
Scala 2 + 2 > 4Scala 2 + 2 > 4
Scala 2 + 2 > 4
 
EmberConf 2021 - Crossfile Codemodding with Joshua Lawrence
EmberConf 2021 - Crossfile Codemodding with Joshua LawrenceEmberConf 2021 - Crossfile Codemodding with Joshua Lawrence
EmberConf 2021 - Crossfile Codemodding with Joshua Lawrence
 
Postgresql 9.3 overview
Postgresql 9.3 overviewPostgresql 9.3 overview
Postgresql 9.3 overview
 
Grammarware Memes
Grammarware MemesGrammarware Memes
Grammarware Memes
 
An Annotation Framework for Statically-Typed Syntax Trees
An Annotation Framework for Statically-Typed Syntax TreesAn Annotation Framework for Statically-Typed Syntax Trees
An Annotation Framework for Statically-Typed Syntax Trees
 
Binary Studio Academy PRO: ANTLR course by Alexander Vasiltsov (lesson 4)
Binary Studio Academy PRO: ANTLR course by Alexander Vasiltsov (lesson 4)Binary Studio Academy PRO: ANTLR course by Alexander Vasiltsov (lesson 4)
Binary Studio Academy PRO: ANTLR course by Alexander Vasiltsov (lesson 4)
 
FNT 2015 PDIS CodeEU - Zanimljiva informatika - 02 Djordje Pavlovic - Live_ch...
FNT 2015 PDIS CodeEU - Zanimljiva informatika - 02 Djordje Pavlovic - Live_ch...FNT 2015 PDIS CodeEU - Zanimljiva informatika - 02 Djordje Pavlovic - Live_ch...
FNT 2015 PDIS CodeEU - Zanimljiva informatika - 02 Djordje Pavlovic - Live_ch...
 
Ast transformations
Ast transformationsAst transformations
Ast transformations
 
#include iostream #include cstring #include vector #i.pdf
 #include iostream #include cstring #include vector #i.pdf #include iostream #include cstring #include vector #i.pdf
#include iostream #include cstring #include vector #i.pdf
 

Recently uploaded

Transform Your Communication with Cloud-Based IVR Solutions
Transform Your Communication with Cloud-Based IVR SolutionsTransform Your Communication with Cloud-Based IVR Solutions
Transform Your Communication with Cloud-Based IVR Solutions
TheSMSPoint
 
UI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
UI5con 2024 - Boost Your Development Experience with UI5 Tooling ExtensionsUI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
UI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
Peter Muessig
 
SMS API Integration in Saudi Arabia| Best SMS API Service
SMS API Integration in Saudi Arabia| Best SMS API ServiceSMS API Integration in Saudi Arabia| Best SMS API Service
SMS API Integration in Saudi Arabia| Best SMS API Service
Yara Milbes
 
How to write a program in any programming language
How to write a program in any programming languageHow to write a program in any programming language
How to write a program in any programming language
Rakesh Kumar R
 
LORRAINE ANDREI_LEQUIGAN_HOW TO USE WHATSAPP.pptx
LORRAINE ANDREI_LEQUIGAN_HOW TO USE WHATSAPP.pptxLORRAINE ANDREI_LEQUIGAN_HOW TO USE WHATSAPP.pptx
LORRAINE ANDREI_LEQUIGAN_HOW TO USE WHATSAPP.pptx
lorraineandreiamcidl
 
Measures in SQL (SIGMOD 2024, Santiago, Chile)
Measures in SQL (SIGMOD 2024, Santiago, Chile)Measures in SQL (SIGMOD 2024, Santiago, Chile)
Measures in SQL (SIGMOD 2024, Santiago, Chile)
Julian Hyde
 
DDS-Security 1.2 - What's New? Stronger security for long-running systems
DDS-Security 1.2 - What's New? Stronger security for long-running systemsDDS-Security 1.2 - What's New? Stronger security for long-running systems
DDS-Security 1.2 - What's New? Stronger security for long-running systems
Gerardo Pardo-Castellote
 
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CDKuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
rodomar2
 
E-commerce Development Services- Hornet Dynamics
E-commerce Development Services- Hornet DynamicsE-commerce Development Services- Hornet Dynamics
E-commerce Development Services- Hornet Dynamics
Hornet Dynamics
 
What is Master Data Management by PiLog Group
What is Master Data Management by PiLog GroupWhat is Master Data Management by PiLog Group
What is Master Data Management by PiLog Group
aymanquadri279
 
SWEBOK and Education at FUSE Okinawa 2024
SWEBOK and Education at FUSE Okinawa 2024SWEBOK and Education at FUSE Okinawa 2024
SWEBOK and Education at FUSE Okinawa 2024
Hironori Washizaki
 
Revolutionizing Visual Effects Mastering AI Face Swaps.pdf
Revolutionizing Visual Effects Mastering AI Face Swaps.pdfRevolutionizing Visual Effects Mastering AI Face Swaps.pdf
Revolutionizing Visual Effects Mastering AI Face Swaps.pdf
Undress Baby
 
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdf
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdfAutomated software refactoring with OpenRewrite and Generative AI.pptx.pdf
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdf
timtebeek1
 
Hand Rolled Applicative User Validation Code Kata
Hand Rolled Applicative User ValidationCode KataHand Rolled Applicative User ValidationCode Kata
Hand Rolled Applicative User Validation Code Kata
Philip Schwarz
 
A Study of Variable-Role-based Feature Enrichment in Neural Models of Code
A Study of Variable-Role-based Feature Enrichment in Neural Models of CodeA Study of Variable-Role-based Feature Enrichment in Neural Models of Code
A Study of Variable-Role-based Feature Enrichment in Neural Models of Code
Aftab Hussain
 
Why Choose Odoo 17 Community & How it differs from Odoo 17 Enterprise Edition
Why Choose Odoo 17 Community & How it differs from Odoo 17 Enterprise EditionWhy Choose Odoo 17 Community & How it differs from Odoo 17 Enterprise Edition
Why Choose Odoo 17 Community & How it differs from Odoo 17 Enterprise Edition
Envertis Software Solutions
 
Oracle Database 19c New Features for DBAs and Developers.pptx
Oracle Database 19c New Features for DBAs and Developers.pptxOracle Database 19c New Features for DBAs and Developers.pptx
Oracle Database 19c New Features for DBAs and Developers.pptx
Remote DBA Services
 
Introducing Crescat - Event Management Software for Venues, Festivals and Eve...
Introducing Crescat - Event Management Software for Venues, Festivals and Eve...Introducing Crescat - Event Management Software for Venues, Festivals and Eve...
Introducing Crescat - Event Management Software for Venues, Festivals and Eve...
Crescat
 
Oracle 23c New Features For DBAs and Developers.pptx
Oracle 23c New Features For DBAs and Developers.pptxOracle 23c New Features For DBAs and Developers.pptx
Oracle 23c New Features For DBAs and Developers.pptx
Remote DBA Services
 
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j - Product Vision and Knowledge Graphs - GraphSummit ParisNeo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j
 

Recently uploaded (20)

Transform Your Communication with Cloud-Based IVR Solutions
Transform Your Communication with Cloud-Based IVR SolutionsTransform Your Communication with Cloud-Based IVR Solutions
Transform Your Communication with Cloud-Based IVR Solutions
 
UI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
UI5con 2024 - Boost Your Development Experience with UI5 Tooling ExtensionsUI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
UI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
 
SMS API Integration in Saudi Arabia| Best SMS API Service
SMS API Integration in Saudi Arabia| Best SMS API ServiceSMS API Integration in Saudi Arabia| Best SMS API Service
SMS API Integration in Saudi Arabia| Best SMS API Service
 
How to write a program in any programming language
How to write a program in any programming languageHow to write a program in any programming language
How to write a program in any programming language
 
LORRAINE ANDREI_LEQUIGAN_HOW TO USE WHATSAPP.pptx
LORRAINE ANDREI_LEQUIGAN_HOW TO USE WHATSAPP.pptxLORRAINE ANDREI_LEQUIGAN_HOW TO USE WHATSAPP.pptx
LORRAINE ANDREI_LEQUIGAN_HOW TO USE WHATSAPP.pptx
 
Measures in SQL (SIGMOD 2024, Santiago, Chile)
Measures in SQL (SIGMOD 2024, Santiago, Chile)Measures in SQL (SIGMOD 2024, Santiago, Chile)
Measures in SQL (SIGMOD 2024, Santiago, Chile)
 
DDS-Security 1.2 - What's New? Stronger security for long-running systems
DDS-Security 1.2 - What's New? Stronger security for long-running systemsDDS-Security 1.2 - What's New? Stronger security for long-running systems
DDS-Security 1.2 - What's New? Stronger security for long-running systems
 
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CDKuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
 
E-commerce Development Services- Hornet Dynamics
E-commerce Development Services- Hornet DynamicsE-commerce Development Services- Hornet Dynamics
E-commerce Development Services- Hornet Dynamics
 
What is Master Data Management by PiLog Group
What is Master Data Management by PiLog GroupWhat is Master Data Management by PiLog Group
What is Master Data Management by PiLog Group
 
SWEBOK and Education at FUSE Okinawa 2024
SWEBOK and Education at FUSE Okinawa 2024SWEBOK and Education at FUSE Okinawa 2024
SWEBOK and Education at FUSE Okinawa 2024
 
Revolutionizing Visual Effects Mastering AI Face Swaps.pdf
Revolutionizing Visual Effects Mastering AI Face Swaps.pdfRevolutionizing Visual Effects Mastering AI Face Swaps.pdf
Revolutionizing Visual Effects Mastering AI Face Swaps.pdf
 
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdf
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdfAutomated software refactoring with OpenRewrite and Generative AI.pptx.pdf
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdf
 
Hand Rolled Applicative User Validation Code Kata
Hand Rolled Applicative User ValidationCode KataHand Rolled Applicative User ValidationCode Kata
Hand Rolled Applicative User Validation Code Kata
 
A Study of Variable-Role-based Feature Enrichment in Neural Models of Code
A Study of Variable-Role-based Feature Enrichment in Neural Models of CodeA Study of Variable-Role-based Feature Enrichment in Neural Models of Code
A Study of Variable-Role-based Feature Enrichment in Neural Models of Code
 
Why Choose Odoo 17 Community & How it differs from Odoo 17 Enterprise Edition
Why Choose Odoo 17 Community & How it differs from Odoo 17 Enterprise EditionWhy Choose Odoo 17 Community & How it differs from Odoo 17 Enterprise Edition
Why Choose Odoo 17 Community & How it differs from Odoo 17 Enterprise Edition
 
Oracle Database 19c New Features for DBAs and Developers.pptx
Oracle Database 19c New Features for DBAs and Developers.pptxOracle Database 19c New Features for DBAs and Developers.pptx
Oracle Database 19c New Features for DBAs and Developers.pptx
 
Introducing Crescat - Event Management Software for Venues, Festivals and Eve...
Introducing Crescat - Event Management Software for Venues, Festivals and Eve...Introducing Crescat - Event Management Software for Venues, Festivals and Eve...
Introducing Crescat - Event Management Software for Venues, Festivals and Eve...
 
Oracle 23c New Features For DBAs and Developers.pptx
Oracle 23c New Features For DBAs and Developers.pptxOracle 23c New Features For DBAs and Developers.pptx
Oracle 23c New Features For DBAs and Developers.pptx
 
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j - Product Vision and Knowledge Graphs - GraphSummit ParisNeo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
 

Pug - a compiler pipeline

Editor's Notes

  1. How many of you use one of these? Raise your hand if you use Webpack or Browserify [Walk] Today I hope to give you a better idea of how these tools work, and give you the tools to build and contribute to them yourself.
  2. Many of us write html every day It's verbose It lacks features for reusability and dynamic content
  3. I maintain a language that used to be called "jade". Today I'm announcing that, along with the release of version 2.0.0, it has been renamed to "pug" Pug is a simple language for producing HTML documents. It has a concise syntax, and supports features for code reuse and dynamic content.
  4. There are three main stages to the pug compiler: [walk] Lex the source code to convert the string of text into a stream of tokens Parse the tokens into a tree structure called an abstract syntax tree Generate output code from the abstract syntax tree
  5. The lexer splits the stream of characters from the source code into logical tokens The first logical token here is the "article" tag, since we read that as a single unit of meaning
  6. The lexer can be thought of as a simple state machine. For our language, it needs to keep track of: The remaining source code that has not yet been lexed. The current level of indentation
  7. We'll define a function for each type of token. Each function will: Match the string against a regular expression If it matches, it will consume those characters Then return a token of the appropriate tag If it does not match, we return `undefined` and don't consume any characters For the tag, we simply match any number of characters in the range a to z
  8. The function for text is mostly the same. We match a space, followed by any characters to the end of the line. We then remove the leading space from the value we store in the token.
  9. Our handling of newlines is a little different. We'll use one function to track indents, outdents and new lines. We match a newline followed by any number of spaces. We then set the number of spaces as the new value of this.indent and compare the new indent against the old indent to decide what token type to return.
  10. To generate the complete stream of tokens we just repeatedly call these methods until there is no more text. Note how if each token type doesn't match, we simply fall through to the next token type. The last token type we consider is `fail`, which just throws an error to indicate that there was some unexpected text. Finally, we push an "end of stream" token, to indicate that there are no more characters to read.
  11. Once the lexer has generated a stream of tokens, like this one, it is the job of the parser to transform that stream of tokens into the logical tree structure that matches how we (as programmers) view the code. [walk] Notice how the "article" contains the "h1" and the "p"
  12. Once the lexer has generated a stream of tokens, like this one, it is the job of the parser to transform that stream of tokens into the logical tree structure that matches how we (as programmers) view the code. [walk] Notice how the "article" contains the "h1" and the "p"
  13. Our parser starts with a stream of tokens
  14. At the top level, we keep looking to parse a new tag until we get to the end of the stream. Start by defining the list of nodes, then while the next token is not "end of stream": - if it's a tag, parseTag - if it's a newline, ignore it
  15. When parsing a tag, we call into `parseText` or `parseBlock` to recursively pass the content of the tag, depending on the type of the next token.
  16. Parsing a block looks just like parsing the file, except that instead of an "end of stream" token, we are looking for an "outdent" token.
  17. Now we have this "abstract syntax tree", we need to generate some output code from it. We'll recursively convert each node of the tree from the bottom up [walk]
  18. The code for this starts with a render method. It will recursively call the appropriate function to render each type of node. Note how these methods mirror the methods of the parser.
  19. To render a block, we simply render all the nodes within it.
  20. To render a tag, we simply build the bits of text that the tag itself provides, and then recursively render the content.
  21. Rendering text is just a case of returning the text
  22. [walk] [emphasis] [smile] Now you have hopefully seen how the parts of the compiler fit together
  23. Pug also supports splitting your code into multiple files. One of the simplest ways it does this is via `includes.
  24. An include results in two separate abstract syntax trees, which must be joined together. To do this without complicating the existing stages of our compiler, we will add an additional stage to our compiler.
  25. An include results in two separate abstract syntax trees, which must be joined together. To do this without complicating the existing stages of our compiler, we will add an additional stage to our compiler.
  26. Lets review the stages of this new compiler pipeline Talk through stages [walk] Clear Separation of Stages Clear Extension Points Uses our own extension points where possible
  27. SMILE!