Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

First working version of grammar #2

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions dev/instaparse_repl.clj
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,19 @@
(def current-parser (insta/parser (io/resource "grammar/ledger.bnf")))
(def proposed-parser (insta/parser (io/resource "grammar/ledger.y")))

(def ledger-file-path "./../test/leedn/fixture.dat")
(def ledger-file-path (io/resource "small-journal.dat"))

(comment
(-> ledger-file-path
io/resource
slurp
proposed-parser
pprint)

(-> ledger-file-path
slurp
current-parser
pprint)

(with-open [r (io/reader (io/resource ledger-file-path))]
(->> r
(line-seq)
Expand Down
218 changes: 90 additions & 128 deletions resources/grammar/ledger.bnf
Original file line number Diff line number Diff line change
@@ -1,133 +1,95 @@
(* ledger grammar file *)
(* vim: set ft=ebnf : *)

<LedgerEntries> = (
CommentHeader
| CommentBlock
| IncludeFile
| AccountDefinition
| CommodityDefinition
| CommodityConversion
| CommodityPrice
| Transaction
)+;


(* Text *)
<NL> = <'\n'> ;
<Indent> = <' '> ;
<Text> = #"\S?[^\n]*\S" ;

(* Dates *)
Date = #"\d{4}-\d\d-\d\d" ;
Time = ( #"\d\d:\d\d" | #"\d\d:\d\d:\d\d" ) , [ <[' ']> , TimeZone ] ;
TimeZone = 'Z' | #"-?\d\d:\d\d" | #"[A-Z][a-zA-Z]+(/[a-zA-Z0-9_-]+)*" ;
DateTime = Date , <( 'T' | ' ' )> , Time ;

(* Numbers *)
<NonZeroDigit> = '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' ;
<Digit> = '0' | NonZeroDigit ;
Number = ['-'] , ( Digit | ( NonZeroDigit , ( Digit | <','> )+ ) ) , [ '.' , Digit+ ] ;
Percentage = Number , <'%'> ;

(* Comments & Includes *)
CommentHeader = <';;;;; '> , #"[^;]*[^ ;]" , <' ;;;;;\n'> ;
CommentBlock = ( <'; '> , Text , NL | <';'> , '\n' )+ , !CommentBlock ;
IncludeFile = <'include '> , #"\S+" , NL ;

(* Standard Metadata *)
TagName = #"[a-zA-Z0-9-]+" ;
MetaTag = <':'> , TagName , <':'> | TagName , <': '> , Text ;
MetaDirective = <'note '> , MetaTag ;
NoteDirective = <'note '> , !MetaTag , Text ;

(* Commodity Definitions *)
CommodityCode = '$' | #"[a-zA-Z][a-zA-Z_]*" | <'"'> , #"[a-zA-Z][a-zA-Z0-9_]*" , <'"'> ;
CommodityDefinition = <'commodity '> , CommodityCode , NL , CommodityDirective* ;
<CommodityDirective> = Indent , ( CommodityFormat | CommodityOption | MetaDirective | NoteDirective ) , NL ;
CommodityFormat = <'format '> , Text ;
CommodityOption = 'nomarket' | 'default' ;

(* Quantities, Conversions, and Prices *)
Quantity = '0' | ( CommodityCode , <[' ']> , Number ) | ( Number , <[' ']> , CommodityCode ) ;
CommodityConversion = <'C '> , Quantity , <' = '> , Quantity , NL ;
CommodityPrice = <'P '> , ( Date | DateTime ) , <' '+> , CommodityCode , <' '+> , Quantity , NL ;

(* Account References *)
<AccountPathWord> = #"[a-zA-Z0-9&][a-zA-Z0-9-.]*" ;
AccountPathSegment = AccountPathWord , ( ' ' , AccountPathWord )* ;
AccountPath = #"[A-Z][a-zA-Z]+" , ( <':'> , AccountPathSegment )* ;
AccountAlias = #"[a-z][a-zA-Z0-9-]*[a-z0-9]" ;
<AccountRef> = AccountPath | AccountAlias ;
RealAccountRef = AccountRef ;
VirtualAccountRef = <'('> , AccountRef , <')'> ;
BalancedVirtualAccountRef = <'['> , AccountRef , <']'> ;

(* Account Definitions *)
AccountDefinition = <'account '> , AccountPath , NL , AccountDirective* ;
<AccountDirective> = Indent , ( AccountAliasDirective | AccountAssertion | MetaDirective | NoteDirective ) , NL ;
AccountAliasDirective = <'alias '> , AccountAlias ;
AccountAssertion = <'assert '> , Text ;

(* History Metadata *)
TimeMetaTag = 'time: ' ;
TimeMeta = <TimeMetaTag> , ( Time | DateTime ) ;

SourceMetaTag = 'source: ' ;
SourceMeta = <SourceMetaTag> , TagName , <'|'> , Text ;

LineItemTag = 'item: ' ;

SpecialMetaTag = TimeMetaTag | SourceMetaTag | LineItemTag ;
MetaEntry = !SpecialMetaTag , MetaTag ;
MetaComment = !( SpecialMetaTag | MetaTag ) , Text ;

(* Transactions *)
Transaction = Date ,
[ <' '> , TxFlag ] ,
[ <' '> , TxCode ] ,
<' '> , !TxFlag , !TxCode , TxMemo , NL ,
TxDetail* ,
Posting+ ;

TxFlag = '!' | '*' ;
TxCode = <'('> , #"[^)]+" , <')'> ;
TxMemo = Text ;
<TxDetail> = Indent , <'; '> , ( TimeMeta | MetaEntry | MetaComment ) , NL ;
<journal> = ledger_entry+ | epsilon

<ledger_entry> = comment
| include
| account_definition
| commodity_definition
| commodity_conversion
| commodity_price
| transaction

(* meta *)
<nl> = <#"\n+">
<white> = <#"[ \t]+">
<text> = #"\S?[^\n]*\S"
<int2> = #"\d{2}"
<int4> = #"\d{4}"

(* dates *)
date = int4 <date_sep> int2 <date_sep> int2 <white?>
date_sep = '/' | '-' | '.'
time = int2 ':' int2 [':' int2]
date_time = date <('t'|'T'|' ')> time

(* numbers *)
amount = #"-?([0-9]|[1-9][0-9]+)(\.|,)[0-9]+"
percentage = amount <'%'>

(* comments & includes *)
(* TODO *)
<comment> = <(';'|'#'|'*')> <text> nl |
<'comment'> (white|nl) {#".*" nl?} <'end comment'>
include = <'include'> <white> text <nl>

(* standard metadata *)
tag_name = #"[a-zA-Z0-9-]+"
meta_tag = <':'> tag_name <':'> | tag_name <': '> text
meta_directive = <'note'> <white> meta_tag
note_directive = <'note'> <white> !meta_tag text

(* commodity definitions *)
commodity_code = '$' | #"[a-zA-Z][a-zA-Z_]*" | <'"'> #"[a-zA-Z][a-zA-Z0-9_]*" <'"'>
commodity_definition = <'commodity'> <white> commodity_code nl commodity_directive*
<commodity_directive> = white (commodity_format | commodity_option | meta_directive | note_directive) nl
commodity_format = <'format'> <white> text
commodity_option = 'nomarket' | 'default'

(* quantities, conversions, and prices *)
quantity = '0' | (commodity_code <white?> amount) | (amount <white?> commodity_code)
commodity_conversion = <'C'> <white> quantity <white> <'='> <white> quantity nl
commodity_price = <'P'> <white> (date | date_time) <white> commodity_code <white> quantity nl

(* account references *)
<account_name> = #"[^: \n]+( [^: \n]+)*"
<account_path> = account_name (<':'> account_name)*
account = account_path
virtual_account = <'('> account <')'>
balanced_virtual_account = <'['> account <']'>

(* account definitions *)
account_definition = <'account'> <white> account_path nl account_directive*
<account_directive> = white (account_alias_directive | account_assertion | meta_directive | note_directive) nl
account_alias_directive = <'alias'> <white> account
account_assertion = <'assert'> <white> text

(* transactions *)
transaction = date
[<white> status]
[<white> code]
<white> !status !code payee nl
posting+

status = '!' | '*'
code = <'('> #"[^)]+" <')'>
payee = text

(* Postings *)
Posting = Indent ,
PostingAccount ,
[ <' '> ,
[ <' '*> , Quantity ,
[ <' '+> , PostingLotCost ,
[ <' '+> , PostingLotDate ] ] ,
[ <' '+> , PostingPrice ] ] ,
[ <' '*> , PostingBalance ] ,
[ <' '*> , <' ; '> , ( MetaEntry | MetaComment ) ] ] ,
NL ,
PostingDetail* ;

<PostingAccount> = RealAccountRef | VirtualAccountRef | BalancedVirtualAccountRef ;
PostingLotCost = <'{'> , Quantity , <'}'> ;
PostingLotDate = <'['> , Date , <']'> ;
PostingPrice = <'@'> , <' '+> , Quantity ;
PostingBalance = <'='> , <' '+> , Quantity ;
PostingDate = <'['> , <['=']> , Date , <']'> ;
<PostingDetail> = Indent , Indent , <'; '> , ( TimeMeta | SourceMeta | PostingDate | LineItem | MetaEntry | !PostingDate , MetaComment ) , NL ;
PostingComment = !PostingDate , Text ;

(* Line Items *)
LineItem = <LineItemTag> ,
#"\S+( \S+)*" ,
[ <' '+> , LineItemTotal ,
[ <' '+> , LineItemDetail ] ,
[ <' '+> , ( LineItemTaxGroups | LineItemTaxApplied ) ] ] ;

LineItemTotal = Quantity ;
<LineItemDetail> = <'('> , <' '*> , LineItemAmount , <' '+> , <'@'> , <' '+> , LineItemPrice , <')'> ;
LineItemAmount = Number | Quantity ;
LineItemPrice = Quantity | Percentage ;
LineItemTaxGroup = #"\*+" ;
LineItemTaxGroups = LineItemTaxGroup , ( <' '> , LineItemTaxGroup )* ;
LineItemTaxApplied = <'<'> , LineItemTaxGroup , <'>'> ;
posting = <white> [status <white>] account posting_amount? <nl>
posting_amount = <' '> <' '+>
[quantity
[<white> posting_lot_cost [<white> posting_lot_date]]
[<white> posting_price]]
[<' '*> posting_balance]

(* TODO *)
amount_expr = quantity | <'('> <')'>
<posting_account> = account | virtual_account | balanced_virtual_account
posting_lot_cost = <'{'> quantity <'}'>
posting_lot_date = <'['> date <']'>
posting_price = <'@'> <white> quantity
posting_balance = <'='> <white> quantity
posting_date = <'['> <'='?> date <']'>
posting_comment = !posting_date text
4 changes: 2 additions & 2 deletions resources/grammar/ledger.y
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ commodity:
string ;

xact: plain_xact |
periodic_xact |
automated_xact ;
periodic_xact |
automated_xact ;

plain_xact:
date date_opt status_opt code_opt fullstring note_opt eol
Expand Down