-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSParser.html
108 lines (73 loc) · 3.88 KB
/
SParser.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>SParser and SParserCalc</title>
<meta name="author" content="Micah Bunting" />
<meta name="description" content="" >
<meta name="keywords" content="mac,apple,iphone,ios,osx,games,apps,development" >
<link rel="alternate" type="application/rss+xml" title="Micah Bunting" href="http://www.delightfuldev.com/feed.xml" />
<link rel="stylesheet" href="/css/component.css">
<link rel="stylesheet" href="/css/demo.css">
<link rel="canonical" href="http://www.delightfuldev.com/SParser">
<link rel="shortcut icon" href="favicon.ico">
<link rel="stylesheet" type="text/css" href="/fonts/feather/style.css">
<link rel="stylesheet" type="text/css" href="/css/animate.min.css">
<script src="/js/modernizr.custom.js"></script>
<script src="/js/hammer.min.js"></script>
</head>
<body>
<div class="container">
<header class="bp-header cf">
</header>
<section class="content content--open">
<div class="content__item content__item--current content__item--reset">
<div class="content__item-inner">
<h2>SParser and SParserCalc</h2>
<h3></h3>
<p><a href="https://github.com/amnykon/SParser">SParser</a> generates a swift parser from a .sparser file. The .sparser contains the rule that describes the format in the file to parse. The format is based on <a href="https://en.wikipedia.org/wiki/Backus–Naur_form">Blocus-Nor-Form</a>.</p>
<p><a href="https://github.com/amnykon/SParserCalc">SParserCalc</a> is a command line calculator that uses SParser and is designed to be an example of how to use SParser.</p>
<p>Rules are used to describe the format of a .sparser file. A rule describes a component in the file that can be read. In <a href="https://github.com/amnykon/SParserCalc/blob/master/Source/SParserCalc/SParserCalc.sparser">SParserCalc.sparser</a>, some of the rules are: expression, term, factor, and number.</p>
<p>Each rule has a type and at least one pattern. Using the expression rule from SParserCalc:</p>
<pre><code>expression
type
Double
::= term
return term
</code></pre>
<p>The type is the Swift data type that is returned when the rule is read. In this case, the return type is Double.</p>
<p>The pattern is a block that starts with “::=”. After the “::=” is a list of other rules to read for this rule. In this case it just reads the term rule. The following line is Swift code that converts the data matched in the pattern to the type of the rule. The expression rule returns the term without modifing it.</p>
<p>There can be multiple patterns in a rule. SParserCalc’s term has 3 patterns.</p>
<pre><code>term
type
Double
::= term "+" factor
return term + factor
::= term "-" factor
return term - factor
::= factor
return factor
</code></pre>
<p>Pattern 1 matches a term rule, a “+” string, followed by a factor rule and returns their sum. This pattern is recursive as the first rule in the pattern is itself.
Pattern 2 is the same as pattern 1, except that it subtracts the factor from the term.
Pattern 3 simply returns the factor.</p>
<p>Once these rules are created, SParser can convert these .sparser files to .swift files. Each rule is turned into a function that will read the contents of the file.</p>
<!--
<div class="footer">
<p>© 2014-2016 Edit this copyright in _includes/copyright.md</p>
</div>
-->
</div>
</div>
<button class="button button--close"><i class="icon icon--circle-cross"></i><span class="text-hidden">Close content</span></button>
</section>
</div>
<section class="content">
</section>
<script src="/js/classie.js"></script>
<script src="/js/dynamics.min.js"></script>
<script src="/js/main.js"></script>
</body>
</html>