Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version
Go to top

Parsing Expression Grammar Support for C# 3.0 Part 1 - PEG Lib and Parser Generator

, 7 Oct 2008
Introduction to the parsing method PEG with library and parser generator
PEG_GrammarExplorer.zip
PEG_GrammarExplorer
Documents
PEG_GrammarExplorer_fromCP-Dateien
PEG_GrammerExplorer.jpg
mssccprj.scc
PEG Explorer
mssccprj.scc
PEG Explorer.csproj.user
Properties
Settings.settings
vssver2.scc
vssver2.scc
PegBase
mssccprj.scc
Properties
vssver2.scc
vssver2.scc
PegSamples
BasicEncodingRules
input
CDURKR2URKR125195
DefiniteLengthForm
CDURKR2URKR125195
hello
TDAUTPTEUR0100011.tap3
vssver2.scc
hello
IndefiniteLengthForm
DefiniteLengthForm
TDAUTPTEUR0100011.tap3
TDAUTPTEUR0100011.tap3
TDAUTPTEUR0100011.tap3
TDAUTPTEUR0100011_withError.tap3
vssver2.scc
vssver2.scc
calc0_direct
input
vssver2.scc
vssver2.scc
calc0_tree
input
vssver2.scc
vssver2.scc
CSharp3
docu
input
vssver2.scc
vssver2.scc
C_KernighanRitchie2
input
vssver2.scc
vssver2.scc
EMail
input
vssver2.scc
vssver2.scc
Json
input
vssver2.scc
peg_template
vssver2.scc
vssver2.scc
mssccprj.scc
PEG Samples.csproj.user
PegGenerator
input
C#
TestCases
C#
.cs
vssver2.scc
vssver2.scc
vssver2.scc
vssver2.scc
Properties
vssver2.scc
python_2_5_2
input
adwords
awapi_python_samples_1.0.0
src
decoratorators_01
vssver2.scc
Problems
Sample PEG Console Parser
PEG Console Parser
input
Properties
Sample PEG Console Parser.csproj.user
vssver2.scc
PEG_GrammarExplorer_Submission.zip
PEG_GrammarExplorer_Submission
PEG_GrammerExplorer.jpg
PEG_GrammerExplorer.zip
#!/bin/env python

# Paginate a text file, adding a header and footer 

import sys, time, string

# If no arguments were given, print a helpful message
if len(sys.argv)!=2:
    print 'Usage: pyprint filename'
    sys.exit(0)

class PrinterFormatter:
    def __init__(self, filename, page_len=58):	
	# Save the time of creation for inclusion in the header
	import time
	self.now=time.asctime(time.localtime(time.time()))

	# Save the filename and page length
	self.filename=filename ; self.page_len = page_len
	
	# Zero all the counters
	self.page=0 ; self.count=0 ; self.header_written=0

    def write_header(self):
	# If the header for this page has just been written, don't
	# write another one.
	if self.header_written: return

	# Increment the page count, and reset the line count
	self.header_written=1 ; self.count=1 ; self.page=self.page+1

	# Write the header
	header=self.filename
	p=str(self.page) ; header=string.ljust(header, 38-len(p))+'['+p+']'
	header=string.ljust(header, 79-len(self.now))+self.now
	sys.stdout.write(header+'\n\n')

    def writeline(self, L):
	# If the line is exactly 80 lines long, chop off any trailing
	# newline, since the printhead will wrap around
        length=len(L)
	if (length % 80) == 0 and L and L[-1]=='\n': L=L[:-1]

	# If we've printed a pageful of lines, output a linefeed and
	# output the header.
	if self.count>self.page_len:
	    sys.stdout.write('')
	    self.write_header()

	# Print the actual line of text
	sys.stdout.write(L) 
	self.count=self.count+1 ; self.header_written=0

# Open the input file, and create a PrinterFormatter object, passing
# it the filename to put in the page header.
    
f=open(sys.argv[1], 'r')
o=PrinterFormatter(sys.argv[1])
o.write_header()			# Print the header on the first page

# Iterate over all the lines in the file; the writeline() method will
# output them and automatically add page breaks and headers where
# required.
while (1):
    L=f.readline()
    if L=="": break
    o.writeline(L)

# Write a final page break and close the input file.
sys.stdout.write('')
f.close()



 

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Martin.Holzherr

Switzerland Switzerland
No Biography provided

| Advertise | Privacy | Mobile
Web02 | 2.8.140916.1 | Last Updated 7 Oct 2008
Article Copyright 2008 by Martin.Holzherr
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid